Solved

Custom Class to take two variables

Posted on 2011-09-14
55
411 Views
Last Modified: 2012-05-12
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
Comment
Question by:jkteater
  • 27
  • 19
  • 9
55 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36540183
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36540191


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

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

there should be "new"
0
 

Author Comment

by:jkteater
ID: 36540192
please show me how to do it correctly
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 36540198
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36540209

>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
 
LVL 47

Expert Comment

by:for_yan
ID: 36540214

There is a good Java tradition to start names of the classes with upper case letter
It is better to make it    RevDataset
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36541333
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
 

Author Comment

by:jkteater
ID: 36542871
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
 

Author Comment

by:jkteater
ID: 36542882
CEHJ I think right now it is safe to assume there is only one revision.  I hope that stays true.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36542947
Your objective is to maintain a dataset together with a revision, so i would expect one parameter to have 'revision' in its name...
0
 

Author Comment

by:jkteater
ID: 36543065
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
 

Author Comment

by:jkteater
ID: 36543077
I really need to get this ArrayList created and start my TableModel Class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36543135
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
 

Author Comment

by:jkteater
ID: 36543221
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36543240

But isn't TYPE also one of the fileds of ItemRev ?
0
 

Author Comment

by:jkteater
ID: 36543251
Yes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36543257
OK, so it might be be better to think of the revision as the principle object, not dataset?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36543262
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36543267
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36543292
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
 

Author Comment

by:jkteater
ID: 36543301
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
 

Author Comment

by:jkteater
ID: 36543325
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36543346
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36543497
>>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
 

Author Comment

by:jkteater
ID: 36543516
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36543637


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
 

Author Comment

by:jkteater
ID: 36543814
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 47

Expert Comment

by:for_yan
ID: 36543925
because you need to have

rds.add(new RevDataset(tcDataset,selectedDataset));
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36543945

 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
 
LVL 47

Expert Comment

by:for_yan
ID: 36543961

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
 
LVL 47

Expert Comment

by:for_yan
ID: 36544006

>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
 
LVL 47

Expert Comment

by:for_yan
ID: 36544030
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
 

Author Comment

by:jkteater
ID: 36544374
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
 

Author Comment

by:jkteater
ID: 36544380
Increasing points for your trouble and patience
0
 

Author Comment

by:jkteater
ID: 36544393
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36544415
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36544427
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36544438
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36544449

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
 

Author Comment

by:jkteater
ID: 36544669
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36544817
This is probably because you are calling add() method before you call the constructor
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36544828
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
 

Author Comment

by:jkteater
ID: 36544867
does that go in the constructor?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36544922
>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
 

Author Comment

by:jkteater
ID: 36544942
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36544985

>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36545146
>>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
 
LVL 47

Expert Comment

by:for_yan
ID: 36545169
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
 

Author Comment

by:jkteater
ID: 36545248
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36545288
>>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
 
LVL 47

Expert Comment

by:for_yan
ID: 36545293
If it is static then you dont need getList()

  ArrayList<RevDataset> aList = EdiSelection.rds;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36545310
>>If it is static ...

I doubt if it should be
0
 

Author Comment

by:jkteater
ID: 36545435
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36546037
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36546058
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now