Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Creating Arraylist of Arraylist

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating Arraylist of Arraylist

    I have an algorithm problem and i have created a class for my objects

    hard problem is how can i create a matrix that has increasing size and every rows in matrix is also increasing

    for example

    matrix is :

    0 1 2 3 4 5 6 7 ....................

    1 object

    2 object object

    3 object object object object object object object

    4 object object object object

    5

    .
    .
    .
    .
    .

    good explanation for matrix : image Untitled.jpg

    every row has increasing size and also it is not clear that howmany rows ll be created

    inside object of matrix has array, int and double value. my first idea is creation of a class name is example_name for objects

    Arraylist<example_name>[] algoritmaIci = new ArrayList[2500]; //this gave a warning
    2500 is just to assign huge size because i guess matrix size wont be bigger than 2500

    or can i create arraylist of arraylist like:


    ArrayList<Arraylist<example_name>> algoritmaIci = new ArrayList<Arraylist<example_name>>();


    what is your other advise for solution?
    Last edited by ms_ceng; December 19th, 2011 at 05:05 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Arraylist of Arraylist

    Yes, you can put arraylists inside of an arraylist.

    Try your code with the compiler and see what happens. Write a small 10-20 line program and experiment with it to get the technique.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Arraylist of Arraylist

    first i tried to create type of string before use my object but i couldnt do it what is my problem in my code?

    my class is:


    import java.util.ArrayList;

    public class ArrayListDeneme {
    public static void main(String args[]) {
    ArrayList<Arraylist<String>> matrix = new ArrayList<Arraylist<String>>();

    Arraylist<String> al = new Arraylist<String>();
    al.add("row 0 col 0");

    al.add("row 2");
    al.add("zhagre");
    al.add("row4");
    al.add("row 3");
    matrix.add(al);
    Arraylist<String> al1 = new Arraylist<String>();
    al1.add("zhagre");
    al1.add("row4");
    al1.add("row 3");
    matrix.add(al1);
    int i = 0;
    int j = 0;
    System.out.print(matrix.size() + " ");

    // display contents of matrix
    for (i = 0; i < matrix.size(); i++) {
    Arraylist<String> gecici = new Arraylist<String>();
    gecici = matrix.get(i);
    for (j = 0; j < gecici.size(); j++) {
    System.out.print(gecici.get(i) + " ");
    System.out.println();
    }
    System.out.println();
    }
    }
    }


    after i wrote this class eclipse advised to add class for inner arraylist and advised to create add() size() and get() method in this inner arraylist class i carried on implement this advise and then my code didnt run

    my inner arraylist class is :


    package yuzswing.denem;

    public class Arraylist<T> {
    Arraylist<String> ekle;
    public Arraylist() {


    }
    public void add(String string) {
    this.ekle.add(string);

    }
    public int size() {
    return this.ekle.size();

    }
    public String get(int i) {
    return this.ekle.get(i);

    }


    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Arraylist of Arraylist

    what is my problem in my code?
    Please explain what problems you are having.
    Does it compile? If not, post the errors.
    Does it execute without errors? If not post the errors.
    Does it give the wrong results? Show the results and explain what is wrong with them.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Arraylist of Arraylist

    Exception in thread "main" java.lang.NullPointerException
    at yuzswing.denem.Arraylist.add(Arraylist.java:10)
    at yuzswing.denem.ArrayListDeneme.main(ArrayListDenem e.java:10)


    when i click Arraylist.java:10 ==> this.ekle.add(string);

    ArrayListDeneme.java:10 ==> al.add("row 0 col 0");


    i just clicked run (Ctrl+F11)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Arraylist of Arraylist

    Exception in thread "main" java.lang.NullPointerException
    at yuzswing.denem.Arraylist.add(Arraylist.java:10)
    at yuzswing.denem.ArrayListDeneme.main(ArrayListDenem e.java:10)
    There is a null variable at line 10 in your program. Look at the code on that line and see what variable is null and then see why it does not have a valid value.
    If you can not tell which variable is null, add a println call just before line 10 and print out the values of all the variables on line 10 so you can see which one is null.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Arraylist of Arraylist

    it is ok so sorry problem is that i wrote Arraylist not ArrayList

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Arraylist of Arraylist

    thanks for greet answer

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Arraylist of Arraylist

    Does the program work now?

  10. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Arraylist of Arraylist

    yes it is working after i changed Arraylist to ArrayList and deleted my Arraylist class

    Now i finished my artificial intelligence lecture homework project but i m getting this exceptions.


    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at matrix.calisiyor.AstarPanel.Diziyiolustur(AstarPan el.java:357)
    at matrix.calisiyor.AstarPanel.coz(AstarPanel.java:28 4)
    at matrix.calisiyor.AstarPanel$5.actionPerformed(Asta rPanel.java:148)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    what is problem in here why i m getting null position error.

  11. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Arraylist of Arraylist

    sorry for huge exceptions

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Arraylist of Arraylist

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at matrix.calisiyor.AstarPanel.Diziyiolustur(AstarPan el.java:357)
    at matrix.calisiyor.AstarPanel.coz(AstarPanel.java:28 4)
    There is a variable on line 357 that has a null value. See post#6

  13. #13
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Arraylist of Arraylist

    356 gidilen=0;
    357 ornek.set_g(gidilen);


    these are snippet of my code yes it has zero value not null i need to set zero value for my zero step in algorithm. why does java suppose that it is null

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Arraylist of Arraylist

    why does java suppose that it is null
    The computer KNOWS there is a variable with a null value. If you can not see what variable is null, you need to add a println statement that prints out the values of all the variables on the line where the exception occurs.
    357 ornek.set_g(gidilen);
    What is the value of ornek?

  15. #15
    Junior Member
    Join Date
    Nov 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating Arraylist of Arraylist

    ornek is an instance of a class, ornek is my object that i defined to use and
    that line one of its values have been set


    i think i found error point there is a part of code below i need to get i and j value of buttons matrix. But i could not access i and j value insede the actionperformed. This create my null value



    private void BlockButtonSec() {
    secim = 2;
    for (final int i = 0; i < 50; i++) {
    for (int j = 0; j < 50; j++) {
    butonlar[i][j].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    System.out.print(i); // icant reach i
    System.out.print(j);
    // while (secim == 2) {
    //
    // if (event.getSource() == butonlar[i][j]) {
    // butonlar[i][j].setBackground(Color.GRAY);
    //
    // koordinatlarengel[index].setCoordinate_x(i);
    // koordinatlarengel[index].setCoordinate_y(j);
    // index++;
    // }
    // }

    }
    });
    }
    }
    }



    System.out.print(i); i cant reach "i" and eclipse advised that make "i value" is a final but after i changed it as a final "for statement" loop create error that final value can not be increased and eclipse advised to delete final it is just like infinite advise cycle

    thank you very much for your attention and concern

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating Arraylist of Arraylist

    What variable is/was null?

Similar Threads

  1. Creating an Intstance in an ArrayList
    By gpelefty90 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2011, 04:54 PM
  2. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  3. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  4. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM