1 Attachment(s)
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 Attachment 912
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?
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.
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);
}
}
Re: Creating Arraylist of Arraylist
Quote:
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.
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)
Re: Creating Arraylist of Arraylist
Quote:
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.
Re: Creating Arraylist of Arraylist
:)>- it is ok so sorry problem is that i wrote Arraylist not ArrayList X_X
Re: Creating Arraylist of Arraylist
thanks for greet answer o:-):o
Re: Creating Arraylist of Arraylist
Does the program work now?
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.
Re: Creating Arraylist of Arraylist
sorry for huge exceptions :(
Re: Creating Arraylist of Arraylist
Quote:
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
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
Re: Creating Arraylist of Arraylist
Quote:
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.
Quote:
357 ornek.set_g(gidilen);
What is the value of ornek?
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 :confused:
thank you very much for your attention and concern :)
Re: Creating Arraylist of Arraylist
What variable is/was null?