# Setting all locations at once in 2D arrays

• 03-20-2009, 06:50 PM
Singing Boyo
Need help with 2D arrays
Hi there.

I am trying to create a 2D array that will originally have all locations in the array set to the same object. Is there any easy way to do this, other than using something repetitive like the code below? I'm sure there must be, but I can't figure it out. Please help!!!
Code:

```Array[0][1]= Object; Array[0][2]= Object; Array[0][3]= Object; Array[0][4]= Object; Array[0][5]= Object; Array[1][0]= Object; Array[2][0]= Object; Array[3][0]= Object; Array[4][0]= Object; Array[5][0]= Object;```
• 03-20-2009, 06:58 PM
Fubarable
Hm, nested loops immediately comes to mind.

Your array indices in your code snippet are wrong. If you did it the long way, you'd do it like so:
Code:

```Array[0][0]= Object; Array[0][1]= Object; Array[0][2]= Object; Array[0][3]= Object; Array[0][4]= Object; Array[1][0]= Object; Array[1][1]= Object; Array[1][2]= Object; Array[1][3]= Object; Array[1][4]= Object;```
• 03-20-2009, 07:17 PM
Singing Boyo
Something Else...
I solved my original problem(using nested loops), and ran into another one...

if my Array is created using
Code:

`int Array[][] = new int [20][20]`
Shouldn't Array[20][20] be the lower right corner of the array? When I try to set the area or use a System.out.println() statement to check what it is set to, I get the java.lang.ArrayIndexOutOfBoundsException. I'm sure there is something I don't understand, so could someone explain it to me?
• 03-20-2009, 07:28 PM
Singing Boyo
Hmm... does java find locations in arrays by the upper left corner of the area? eg. if I had a 8 by 8 chess board, the bottom right square would be ChessBoard[7][7] in java? plz tell me if I'm right
• 03-20-2009, 08:24 PM
Singing Boyo
Ok, I looked at more examples, and figured out my problem... and now I have another one.

Is there any way to create a large number of JLabels quickly? I want a 20 by 20 Array of JLabels. Using the standard JLabel myLabel = new JLabel method to create 400 JLabels would take forever. Is there a simpler way of doing this?
• 03-20-2009, 09:43 PM
angryboy
Quote:

Is there any way to create a large number of JLabels quickly?
what's so difficult about using a nested loop. just use nested loop to create 400 of them.
• 03-20-2009, 09:57 PM
Singing Boyo
400 JLabels? can nested loops actually do that? I know you can use loops to draw Graphics objects and set Arrays. I've never heard of them being used for JLabels though... could you post an example?
• 03-20-2009, 10:07 PM
angryboy
Code:

```JLabel[][] jlbArray = new JLabel[20][20] for(int i =0; ...)   for(int j=0; ...)     jlbArray[i][j] = new JLabel();```
only problem is labeling it. i figured anyone who wants 400 labels is prob using it as demo.
• 03-20-2009, 10:20 PM
Singing Boyo
Quote:

Originally Posted by angryboy
Code:

```JLabel[][] jlbArray = new JLabel[20][20] for(int i =0; ...)   for(int j=0; ...)     jlbArray[i][j] = new JLabel();```

I don't need to do much with the JLabels, but I do need to modify them when something changes on an Array. If all the JLabels all have the same possible images on them, is it possible to create something relatively simple to do this?
• 03-20-2009, 10:58 PM
angryboy
be creative. pass in another array.
Code:

```JLabel[][] jlbArray = new JLabel[20][20] for(int i =0; ...)   for(int j=0; ...)     jlbArray[i][j] = new JLabel(s[i][j]);```
where s is a 2d array of Strings or Icon.
• 03-21-2009, 05:17 AM
Singing Boyo
Can't make anything but a blank frame appear now...
Ok... so I am creating the arrays and I can trace them with println statements, but I can't get the JLabels to show on the screen, all I get is a blank frame. Any idea what I am doing wrong? All ImageIcon strings are declared in a separate class,(Icons) but as I said, it traces to the correct file when checked with println, so no problems there. This is my code:
Code:

```package testarea; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame; public class JLabelArray extends JPanel{         private int test1, test2;         private ImageIconArray TestNavArray = new ImageIconArray();         protected JLabel[][] Array = new JLabel[20][20];                         public JLabelArray(){                                 setLayout(null);                 for (test1 = 0; test1 < 20; test1++)                         for (test2 = 0; test2 <20; test2++)                                 Array[test1][test2] = new JLabel(TestNavArray.IArray[test1][test2]);                         System.out.println(Array[5][5]);                         for (test1 = 0; test1 < 20; test1++)                                 for (test2 = 0; test2 <20; test2++)                 add(Array[test1][test2]);//<<<<<new code!!!>>>>>                 }         public static void main(String[]args){                                 JLabelArray testLoop = new JLabelArray();                 JPanel testPanel = new JPanel();                 testPanel.add(testLoop);                 JFrame testFrame = new JFrame();                 testFrame.setContentPane(testPanel);                 testFrame.setSize(600,600);                 testFrame.setVisible(true);                         } }```
Code:

```package testarea; import javax.swing.ImageIcon; public class ImageIconArray {         ImageIcon[][]IArray= new ImageIcon[20][20];                public ImageIconArray(){                         ImageIcon myIcon = new ImageIcon();                 for (int setter =0; setter < 20; setter++)                         for (int setter2 = 0; setter2 < 20; setter2++)                                        IArray[setter][setter2]= myIcon;                         //All the statements below returned the correct filepath to the                 //image I wanted                 System.out.println(IArray[5][5]);                 System.out.println(IArray[6][5]);                 System.out.println(IArray[2][5]);                 System.out.println(IArray[9][5]);                 System.out.println(IArray[15][5]);                } }```
• 03-21-2009, 05:23 AM
Fubarable
the code above does not compile. Too many dependencies that we are not privy to.
• 03-21-2009, 07:13 AM
angryboy
couple of things. you did not add any of the JLabel in the array to the panel. also, if you're trying to fill the array w/ the same object reference, just use Arrays.fill(Object[] a, Object val).

and, like fubarable pointed out: "Too many dependencies", what is pics.Icons ?
• 03-22-2009, 12:27 AM
Singing Boyo
pics.Icon is a class I used to set all the Icons for the entire project. I was finding that I had a lot of classes needed the same Images, and I found it easier to create them all in one class. I edited my code so it should compile for you. I also added the array to my JPanel. I did make that addition obvious. Still comes up blank and my println statements find the right file.
• 03-22-2009, 11:49 PM
angryboy
ImageIcon myIcon = new ImageIcon();
its not showing up because you don't have any images added.