Results 1 to 12 of 12

Thread: ArrayList

  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default ArrayList

    I want to have a helper class to my main class. In the helper class I want to create objects and store them in the first class ArrayList of 2nd class's objects. But, I want each object to have different amount of variables and type of variables. So basically I want an ArrayList as follow

    ArrayList<otherClass>

    position [0] would be int a, int b
    position [1] let's say String a, JLabel b
    position [2] let's say Jlabel a, b, c, d, e, f

    Is something like this possible?

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: ArrayList

    You can't add different data types to one ArrayList, use Vector instead.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: ArrayList

    Quote Originally Posted by milovan View Post
    You can't add different data types to one ArrayList, use Vector instead.
    Yes you can, the Object class is the mother of all classes so each class/type is an Object; ArrayLists can store Object elements. It's all the same with Vectors.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: ArrayList

    @Jos
    In the end you are right as always, but do you think it is clear enough to OP

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: ArrayList

    There is one other approach to this task.

    You can have one class for all data

    Java Code:
    class Helper {
        int a;
        String s;
        double d;
    
        // encapsulate all these fields with set() and get() methods, for example:
        public int getInt(){
            return a;
        }
    now you can use it within ArrayList in any class

    Java Code:
    class TestClass{
        ArrayList<Helper> array = new ArrayList<Helper>();
        // populate array with various Helper objects
    
        // now you can get wanted elements for example:
        int x = array.get(somePosition).getInt();

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: ArrayList

    @milovan: that is overkill; have a look at this:

    Java Code:
    import java.util.ArrayList;
    
    public class T {     
    	public static void main (String[] args) {
    		ArrayList<Object> l= new ArrayList<Object>();
    		
    		l.add(42);
    		l.add(54.0);
    		l.add("foo");
    		
    		System.out.println(l);
    	}
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: ArrayList

    As I said you are right, OP have to take care in what to cast objects from arraylist, but it wolud be the same with vector, so again you are right. My second example is more in line with OP first post, with the task. I'm not sure if I understand it correctly but he mentioned two classes, one to "create objects" and to store them in second class (object) itc. My code would be one of possible directions if applicable...

  8. #8
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: ArrayList

    I think OP meant an array list that contains array lists of sets of variables...

    Java Code:
    ArrayList<ArrayList<Object>> lList;
    ...
    int i = (int)lList.get(intArrayIndex).get(inIndex);
    String s = (String)lList.get(stringArrayIndex).get(strIndex);
    ...
    Last edited by k1ng; 04-28-2012 at 11:33 AM. Reason: spelling...BAD

  9. #9
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: ArrayList

    thanks for the info....still in a little bit of doubt, let me explain further

    main class has a JPanel with a cardlayout. Each card will have multiple options (let's say one card will have 5 checkboxes, 3jbuttons, 2textfields), a 2ndcard will have (19Jcheckboxes) a 3rd card will have (5 radio buttons)

    So idea is to have that 2nd class create the contents of a card so that the code is not bloated with hundreds of checkboxes etc. The 2nd class identifies the string parameter passed to it and based on the string parameter calls method that contains the contents of a card.

  10. #10
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: ArrayList

    Ok. I got what I wanted

    it's working how i want now......thanks for the help again!

  11. #11
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: ArrayList

    It's me again.....one more question.....How do I do add the actionlistener to the below???

    Java Code:
    public void cardInformation(JPanel panel1, String exname) {
    	   
    	   
    	   getInfo = new SupportingDoc(exname);
    	   ArrayList<JComponent> arrays = getInfo.getArray();
    	   for(JComponent eachcomp : arrays) {
    		   panel1.add(eachcomp);
    		   eachcomp.addActionListener(textFieldListener);
    		   /*above line doesn't work The method addActionListener(ActionListener) is undefined for the type JComponent
    		    * So how would I add listeners to all the JComponents now? some are jbuttons, some are jtextfields, Jcheckboxes???
    		    */
    		   
    	   }
    	   
    	   
       }
    Edit... I think i need to convert from JComponent back to Jtextfield/jlabel etc...right? would i use getUIClassID()?
    Last edited by Danieldcc; 04-28-2012 at 08:55 PM.

  12. #12
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: ArrayList

    Ok. I think I got it........works fine....does it look right?

    Java Code:
    public void cardInformation(JPanel panel1, String exname) {
    	   
    	   
    	   getInfo = new SupportingDoc(exname);
    	   ArrayList<JComponent> arrays = getInfo.getArray();
    	   for(JComponent eachcomp : arrays) {
    		   
    		   if(eachcomp.getUIClassID().equals("ButtonUI")) {
    			   panel1.add((JButton) eachcomp);
    			   ((JButton) eachcomp).addActionListener(textFieldListener);
    		   }
    		   else if(eachcomp.getUIClassID().equals("TextFieldUI")) {
    			   panel1.add((JTextField) eachcomp);
    		   }
    		   else if(eachcomp.getUIClassID().equals("LabelUI")) {
    			   panel1.add((JLabel) eachcomp);
    		   }
    		   else if(eachcomp.getUIClassID().equals("CheckBoxUI")) {
    			   panel1.add((JCheckBox) eachcomp);
    		   }
    	   }
       }

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  3. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •