Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Custom Class to take two variables

In a earlier question this was a suggestion.

You can create your custom class which will have two fields - dataset and revision and then make a set or ArrayList made uo of the instances iof your custom class

So you define your class:

class MyClass {
String data;
String rev:
MyClass(String s1, String s2){
data = s1;
rev = s2;
}
}

ArrayList <MyClass> ar = new ArrayList<MyClass>();

As you parse you create your objects and add them to ArrayList

ar.add(new MyClass(data,rev));

I have decided to try and make this work.  

I have created a class named revDataset.
 
public class revDataset {
	
	TCComponent rev;
	TCComponent dataset;
	  
	/**
	 * The constructor
	 */
	public revDataset(TCComponent tcDataset, TCComponent selectedDataset) {
	rev = tcDataset;
	dataset = selectedDataset;
	}
	
	
} // end class revDat

Open in new window

tcDataset, selectedDataset are the objects I want to put in a list  and I am getting those values from a class file Named edihandler

 
try {
	        		selectedRevisions =  selectedDataset.whereReferencedByTypeRelation(dataType, dataRelation);
	        		int size = selectedRevisions.length;
	        		
					
					if (size == 1) {
					TCComponent tcDataset = (TCComponent) selectedRevisions[0].getComponent();
					   
					   ediSelection.add(tcDataset,selectedDataset);
					   					   
					   
					}
		            else {
		        		System.out.println("Terminate\n");
		        	    continue;
		        	}

Open in new window

I was trying to write a add method in a seperate class to place the objects in my list
ediSelection.add(tcDataset,selectedDataset);

Here is the ediSelection class
 
public class ediSelection {
	TCComponent rev;
	TCComponent dataset;
	//static Map <TCComponent, TCComponent> map = new HashMap<TCComponent, TCComponent>(); 
	static List<TCComponent> rds;  
	/**
	 * The constructor
	 */
	private ediSelection() {
	 
	}
	
	public static void add(TCComponent tcDataset, TCComponent selectedDataset){
		rds.add(revDataset(tcDataset,selectedDataset));
		
		//System.out.println("Key :" + tcDataset.toString() + " Value : " + selectedDataset.toString() + " \n");
		//map.put(tcDataset,selectedDataset);
		
	}

Open in new window


Please look over what I have thus far and see if you can help me finish this.  I am now sure how the ediSelection class should work.

I hope I have explained this well enough and please use my code to explain to me how to get this working.
0
jkteater
Asked:
jkteater
  • 27
  • 19
  • 9
1 Solution
 
for_yanCommented:
I think one important thing which you should add to you class revDataset to make ituseful
is to have get methods - you shoudl be able to get back each of the two compounenet from the instance of your two-component class
0
 
for_yanCommented:


>rds.add(revDataset(tcDataset,selectedDataset));

this should be I guess
rds.add(new revDataset(tcDataset,selectedDataset));

there should be "new"
0
 
jkteaterAuthor Commented:
please show me how to do it correctly
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
This is how you add get.. methods - because after you
use this class to put in the ArrayList and pass it somewhere you'll ecentually need rthe way to get
back indivual paramneters

class MyClass {
String data;
String rev:
MyClass(String s1, String s2){
data = s1;
rev = s2;
}

public String getData() {
return data;
}

public String getRev() {
return rev;
}
}

Open in new window

0
 
for_yanCommented:

>static List<TCComponent> rds;  


This should be

static ArrayList<revDataset> rds;

And in the constructoir you need to initialize your array list:
private ediSelection() {
      
rds = new ArrayList<revDataset>();
      }

Otherwise you would not be able to add elments to this ArrayList.

0
 
for_yanCommented:

There is a good Java tradition to start names of the classes with upper case letter
It is better to make it    RevDataset
0
 
CEHJCommented:
Don't forget that will only allow one revision, and though that's possibly all that's required at the moment, your code will break if multiple revisions are allowed later.
0
 
jkteaterAuthor Commented:
OK from there what would the add method look like in the edlSelection

public static void add(TCComponent tcDataset, TCComponent selectedDataset){
            rds.add(tcDataset,selectedDataset);
            
}

also is this the right way to send the data to put in the array (this is the class that gets the data)

ediSelection.add(tcDataset,selectedDataset);
0
 
jkteaterAuthor Commented:
CEHJ I think right now it is safe to assume there is only one revision.  I hope that stays true.
0
 
CEHJCommented:
Your objective is to maintain a dataset together with a revision, so i would expect one parameter to have 'revision' in its name...
0
 
jkteaterAuthor Commented:
Yes I should have named tcDataset something like selectedRevsion.

I am so new at this.  I am not only fairly new at Java with UI (panels, jframe etc...)  I am still new to the application I am writing the code for.  And I have a very short time to get it coded and tested and pushed to production.
0
 
jkteaterAuthor Commented:
I really need to get this ArrayList created and start my TableModel Class.
0
 
CEHJCommented:
I'm just hoping you've got it clear in your mind. If you haven't, you can't code it properly and if we code it for you, you can't maintain it properly.

Let us agree: the primary objective is to maintain a list of mappings between datasets and their revision?
0
 
jkteaterAuthor Commented:
The user selects a TYPE, i am calling that dataset
TYPE is a attribute of a object ItemRev - There is a check in the code to make sure that the TYPE picked only has one ItemRev.  I am calling the ItemRev (revision).  I am trying to keep the TYPE and ItemRev together and stored in a list.  I will pass my List to a Jtable where I will get other attributes from the Object ItemRev and populate my table.
0
 
for_yanCommented:

But isn't TYPE also one of the fileds of ItemRev ?
0
 
jkteaterAuthor Commented:
Yes
0
 
CEHJCommented:
OK, so it might be be better to think of the revision as the principle object, not dataset?
0
 
for_yanCommented:
Id so, you don't need any new class.

Just create

ArrayList<ItemRev> ar = new ArrayList<ItemRev>();

and then add to this list yuour ItemRev objects

ar.add(ItemRev);

Then you pass this arraylist (named ar here) to you class which ccretae tablemodel
0
 
CEHJCommented:
It's of paramount importance to get this clarified or you'll end up with a mess, preceded by a lot of wasted time
0
 
for_yanCommented:
and then you make

DefaultTableModel model = neew DefaultTableModel();

model.addColumn("Itme Id");
model.addComun("TYPE");
.... all other fileds of ItemRev which you want to show

then you iterate thorout your ArrayList:

for(ItemRev item: ar){

Vector<String> v = new Vector<String>();

v.addElement(item.getRevId();
v.addElement(item.getType();
.....


//when you are don with all fileds

model.addRow(v);


}

jtable.setModel(model);

0
 
jkteaterAuthor Commented:
I already have the class written and most of the code in place.  I need to know how to code the add method in my EdiSelection class and how to call the add method with the objects from my EdiHandler class
0
 
jkteaterAuthor Commented:
I am sure I will be creating a new question for the JTABLE stuff - but right now I have to get my list completed with the data
0
 
for_yanCommented:
Do you have somewhere your object ItemRev - which has all these properties including TYPE, etc. defined?
How do you call it in your code - I don't see it
0
 
CEHJCommented:
>>I am sure I will be creating a new question for the JTABLE stuff

Yes, JTable and the like irrelevant at the moment

Can you confirm 'dataset' and 'revision' are of the same type (TCComponent) in the real code?
0
 
jkteaterAuthor Commented:
for (Iterator<?> iter = structSelection.iterator(); iter.hasNext();) {
                     AIFComponentContext element =  (AIFComponentContext) iter.next();
                     TCComponent selectedDataset = (TCComponent) element.getComponent();
                     //TCComponent selectedDatasetType = selectedDataset.getTypeComponent();
                     AIFComponentContext[] selectedRevisions;
                     try {
                          selectedRevisions =  selectedDataset.whereReferencedByTypeRelation(dataType, dataRelation);
                          int size = selectedRevisions.length;
                          //objectsSelected.append("Size: " + size + "\n");
                          System.out.println("size" + size + "\n");
                              //objectsSelected.append("Parent Type:  " + Arrays.toString(selectedRevisions) + "\n");
                              if (size == 1) {
                              TCComponent tcDataset = (TCComponent) selectedRevisions[0].getComponent();
0
 
for_yanCommented:


So suppose I have an instance of TCComponent - can I get form this instance TYPE, ItemID, etc.

Say can I say

TCComponent selectedDataset;

String TYPE  = selecetdDataset.getTYPE();
String ItemID = selectedDataset.getItemID();

etc.

Will that work?
Do you have these methods?

0
 
jkteaterAuthor Commented:
I am really still trying to pass the data in to my list.

Class to keep the values together

 
package com.lexmark.ediua.dialogs;


import java.util.*;

import com.teamcenter.rac.kernel.TCComponent;


public class RevDataset {
	
	TCComponent rev;
	TCComponent dataset;
	  
	/**
	 * The constructor
	 */
   public RevDataset(TCComponent tcDataset, TCComponent selectedDataset) {
      rev = tcDataset;
	  dataset = selectedDataset;
   }
	
   public TCComponent getDataset() {
      return dataset;
   }

   public TCComponent getRev() {
      return rev;
   }
} // end class revDataset

Open in new window


This where I am trying to pass the values to the class ediSelect to make my list

 
TCComponent tcDataset = (TCComponent) selectedRevisions[0].getComponent();
					   	
					   ediSelection.add(tcDataset,selectedDataset);  <===== CALL
					   
					}

Open in new window


This is the class I am trying to use to create the list and add the values being passed in from the above code

 
public class EdiSelection {
	TCComponent rev;
	TCComponent dataset;
	//static Map <TCComponent, TCComponent> map = new HashMap<TCComponent, TCComponent>(); 
	static List<TCComponent> rds;  
	/**
	 * The constructor
	 */
	private EdiSelection() {
	 
	}
	
	public static void add(TCComponent tcDataset, TCComponent selectedDataset){
		rds.add(RevDataset(tcDataset,selectedDataset));
		
		//System.out.println("Key :" + tcDataset.toString() + " Value : " + selectedDataset.toString() + " \n");
		//map.put(tcDataset,selectedDataset);
		
	}
	
	//public static Map<TCComponent, TCComponent> getMap() {
	//	
	//   return map;
	//} // end getMap()

} // end class ediSelection

Open in new window


I am getting errors

public static void add(TCComponent tcDataset, TCComponent selectedDataset){
            rds.add(RevDataset(tcDataset,selectedDataset)); <==== Error with RevDataset
      }


Do I have to have a method to return the list from EdiSelection?  But right now I just want to populate the list

0
 
for_yanCommented:
because you need to have

rds.add(new RevDataset(tcDataset,selectedDataset));
0
 
for_yanCommented:

 public RevDataset(TCComponent tcDataset, TCComponent selectedDataset) {
      rev = tcDataset;
        dataset = selectedDataset;
   }


This method is the constructor of your class (constructor is the method
has the same name as a class , and does not have return type in decalaration - this method in fact creates the instance of the class)
When you want to execute a cnostructor then you need to put the wiord new in fornt fo the method call


0
 
for_yanCommented:

In this operation :
rds.add(new RevDataset(tcDataset,selectedDataset));

you craete an instance of RevDataset by calling its constructor
new RevDataset(tcDataset,selectedDataset);
and then you add this instance to your ArrayList

0
 
for_yanCommented:

>Do I have to have a method to return the list from EdiSelection?

Your rds list is a static variable, so you can access it
jsut in this way EdiSelection.rds
- you don't need a method.

Declare it rather as ArrayList - irt will be simpler

As I mentioned avbove you need to isntaiate your list before firts time you call ad, like that:


public class EdiSelection {
	TCComponent rev;
	TCComponent dataset;
	//static Map <TCComponent, TCComponent> map = new HashMap<TCComponent, TCComponent>(); 
	static ArrayList<TCComponent> rds;  
	/**
	 * The constructor
	 */
	private EdiSelection() {
                 rds = new ArrayList();
	 
	}
	
	public static void add(TCComponent tcDataset, TCComponent selectedDataset){
		rds.add(RevDataset(tcDataset,selectedDataset));
		
		//System.out.println("Key :" + tcDataset.toString() + " Value : " + selectedDataset.toString() + " \n");
		//map.put(tcDataset,selectedDataset);
		
	}
	
	//public static Map<TCComponent, TCComponent> getMap() {
	//	
	//   return map;
	//} // end getMap()

} // end class ediSelection

Open in new window


0
 
for_yanCommented:
Why do you need to have a private constructor in this EdiSelection?

This would not allow you to create EdiSelection with new EdiSelection() from another class.

UUnless you have a good reason - don't do that - remove "private" form line

private EdiSelection() {...  --->

just

EdiSelcetion() {
...

}

would be fine


0
 
jkteaterAuthor Commented:
OK here is the EdiSelection Class - I am getting this error on the line

rds.add(new RevDataset(tcDataset,selectedDataset)); Error is referring to the add

The method add(TCComponent) in the type List<TCComponent> is not applicable for the arguments (RevDataset)

It suggest doing a addAll instead of add


 
public class EdiSelection {
	TCComponent rev;
	TCComponent dataset;
	TCComponent tcDataset;
	TCComponent selectedDataset;
	static List<TCComponent> rds;  
	/**
	 * The constructor
	 */
	EdiSelection() {
		rds = new ArrayList();
		new RevDataset(tcDataset,selectedDataset); 
	}
	
	public static void add(TCComponent tcDataset, TCComponent selectedDataset){
		rds.add(new RevDataset(tcDataset,selectedDataset));
		
		System.out.println("Key :" + tcDataset.toString() + " Value : " + selectedDataset.toString() + " \n");
		
		
	}

Open in new window

0
 
jkteaterAuthor Commented:
Increasing points for your trouble and patience
0
 
jkteaterAuthor Commented:
RevDataset class

 
public class RevDataset {
	
	TCComponent rev;
	TCComponent dataset;
	  
	/**
	 * The constructor
	 */
   public RevDataset(TCComponent tcDataset, TCComponent selectedDataset) {
      rev = tcDataset;
	  dataset = selectedDataset;
   }
	
   public TCComponent getDataset() {
      return dataset;
   }

   public TCComponent getRev() {
      return rev;
   }
} // end class revDataset

Open in new window

0
 
for_yanCommented:
public class EdiSelection {
	TCComponent rev;
	TCComponent dataset;
	TCComponent tcDataset;
	TCComponent selectedDataset;
	static ArrayList<RevDataset> rds;  
	/**
	 * The constructor
	 */
	EdiSelection() {
		rds = new ArrayList();
		//new RevDataset(tcDataset,selectedDataset); 
	}
	
	public static void add(TCComponent tcDataset, TCComponent selectedDataset){
		rds.add(new RevDataset(tcDataset,selectedDataset));
		
		System.out.println("Key :" + tcDataset.toString() + " Value : " + selectedDataset.toString() + " \n");
		
		
	}

Open in new window

0
 
for_yanCommented:
See how I modified, you need to declare ArrayList<RevDataset> rds

If you declare it as ArrayList of TCComponent and then want to add enetity iof different class - that's where it causes an error
0
 
for_yanCommented:
Even beetr this - in the constrictor I also put
rds = new ArrayList<RevDataset>();


public class EdiSelection {
	TCComponent rev;
	TCComponent dataset;
	TCComponent tcDataset;
	TCComponent selectedDataset;
	static ArrayList<RevDataset> rds;  
	/**
	 * The constructor
	 */
	EdiSelection() {
		rds = new ArrayList<RevDataset>();
		//new RevDataset(tcDataset,selectedDataset); 
	}
	
	public static void add(TCComponent tcDataset, TCComponent selectedDataset){
		rds.add(new RevDataset(tcDataset,selectedDataset));
		
		System.out.println("Key :" + tcDataset.toString() + " Value : " + selectedDataset.toString() + " \n");
		
		
	}

Open in new window

0
 
for_yanCommented:

You don't need to have new RevDataset(tcDataset,selectedDataset);
in the constructor of EdiSelection - in the constructor you just create empty list which you'll populate
through calling add method later
0
 
jkteaterAuthor Commented:
rds.add(new RevDataset(tcDataset,selectedDataset));

throwing a

Unhandled event loop execption java.lang.NullPointerException

See if this makes sense

public class EdiSelection {
      TCComponent rev;
      TCComponent dataset;
            
      static List<RevDataset> rds;   Changed from static List<TCComponent> to what you see there  fixed the error with the add
      
      
       /*The constructor */
      EdiSelection() {
            rds = new ArrayList<RevDataset>();
            
      }

//////////////////////////////////////////////////////////////////////////
//                                                                      //
//                            add()                                     //
//                                                                      //
//////////////////////////////////////////////////////////////////////////
      
      public static void add(TCComponent tcDataset, TCComponent selectedDataset){
            rds.add(new RevDataset(tcDataset,selectedDataset));

When I made that change it fixed the error with the add, but now it is throwing null pointer
0
 
for_yanCommented:
This is probably because you are calling add() method before you call the constructor
0
 
for_yanCommented:
From the code where you are collecting your data you first need to create Ediselection

EdiSelection es = new EduiSelection();
es.add(...)

If you add bbefore you create - you'll have that null pointer
0
 
jkteaterAuthor Commented:
does that go in the constructor?
0
 
for_yanCommented:
>does that go in the constructor? - what that means?

In the construyctor you create the instance of your ArrayList
then in the next step you can add to ediSelection which in fact will mean that
you add to the rds ArrayList which you created

If you don't call constructior  - you'll not craete the ArrayList and then
when you try to add something to null (as it aws not created before)
then you have this null pointer
0
 
jkteaterAuthor Commented:
Last question of the day - That seems to be working now

How do I get my list in another class?

is it something like this?

ArrayList<RevDataset> aList = new ArrayList<RevDataset>();
0
 
for_yanCommented:

>How do I get my list in another class?



>ArrayList<RevDataset> aList = new ArrayList<RevDataset>();

one has nothing to do with another:

>ArrayList<RevDataset> aList = new ArrayList<RevDataset>();  this just craetes and instance of ArrayList<RevDataset> named aList

In order to get access to this instance from another class
you need to have get or set method or you need to have this instanca
as parameter for the constructir when you create that another class
we already talked about all that when passing the Map

Passing any kind of object - is it a Map,  an ArrayList or just String - is always done in the same way - look
at those discussion in your previous question




0
 
CEHJCommented:
>>static ArrayList<RevDataset> rds;  

I very much doubt that should be static. That would mean that each an every instance of EdiSelection would have the same List 'rds'
0
 
for_yanCommented:
Yes,  I agree with CEHJ - it is strange that you have it static.

So if you don't have it staic - you'd probably indeed waht to have
a method , say getList()

public ArrayList<Revdataset> getList() {
return rds;

}
0
 
jkteaterAuthor Commented:
I was told it has to be a static list mainly because the values are coming from a handler class which is a instance class.

public EdiBaseDialog(){  <== Constructor
         ArrayList<RevDataset> aList = EdiSelection.getList();

Does that look right?
0
 
CEHJCommented:
>>I was told it has to be a static list mainly because the values are coming from a handler class which is a instance class.

Not sure what that means, but it doesn't sound relevant to the static issue ;)
0
 
for_yanCommented:
If it is static then you dont need getList()

  ArrayList<RevDataset> aList = EdiSelection.rds;
0
 
CEHJCommented:
>>If it is static ...

I doubt if it should be
0
 
jkteaterAuthor Commented:
private void printList(ArrayList aList) {
      
      int size = aList.size();
      System.out.println("Size : " + size + "\n");
      Iterator listIterator = aList.iterator();
      while(listIterator.hasNext())
      {
      Object element = listIterator.next();
      System.out.println(element.toString());
      }
 
This is method I am using to print the list out.  But it is printing out what looks like memory addresses  
0
 
for_yanCommented:
You need to provide method toString() in your implementation of
RevDataset class - see below:

public class RevDataset {
	
	TCComponent rev;
	TCComponent dataset;
	  
	/**
	 * The constructor
	 */
   public RevDataset(TCComponent tcDataset, TCComponent selectedDataset) {
      rev = tcDataset;
	  dataset = selectedDataset;
   }
	
   public TCComponent getDataset() {
      return dataset;
   }

   public TCComponent getRev() {
      return rev;
   }

public String toString(){

return ("dtaset: " + dataset.toString() + "  Rev:  " + rev.toString());

}

} // end class revDataset

Open in new window

0
 
for_yanCommented:
I was assuming that you TCComponent also has public String toString()
method. If not, you need to define that method either

and you printList method should be like that:

private void printList(ArrayList <RevDataset> aList) {
      
      int size = aList.size();
      System.out.println("Size : " + size + "\n");
      Iterator listIterator = aList.iterator();
      while(listIterator.hasNext())
      {
      RevDataset element = listIterator.next();
      System.out.println(element.toString());
      }

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 27
  • 19
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now