Results 1 to 15 of 15
- 03-20-2009, 05:50 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!!!
Java 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;
Last edited by Singing Boyo; 03-20-2009 at 08:26 PM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
-
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:
Java 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, 06:17 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Something Else...
I solved my original problem(using nested loops), and ran into another one...
if my Array is created usingShouldn'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?Java Code:int Array[][] = new int [20][20]
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 03-20-2009, 06:28 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 03-20-2009, 07:24 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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?If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 03-20-2009, 08:43 PM #6
what's so difficult about using a nested loop. just use nested loop to create 400 of them.Is there any way to create a large number of JLabels quickly?USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 03-20-2009, 08:57 PM #7
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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?
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 03-20-2009, 09:07 PM #8
only problem is labeling it. i figured anyone who wants 400 labels is prob using it as demo.Java Code:JLabel[][] jlbArray = new JLabel[20][20] for(int i =0; ...) for(int j=0; ...) jlbArray[i][j] = new JLabel();USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 03-20-2009, 09:20 PM #9
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 03-20-2009, 09:58 PM #10
be creative. pass in another array.
where s is a 2d array of Strings or Icon.Java Code:JLabel[][] jlbArray = new JLabel[20][20] for(int i =0; ...) for(int j=0; ...) jlbArray[i][j] = new JLabel(s[i][j]);USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 03-21-2009, 04:17 AM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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:
Java 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); } }Java 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]); } }Last edited by Singing Boyo; 03-21-2009 at 11:27 PM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
-
the code above does not compile. Too many dependencies that we are not privy to.
- 03-21-2009, 06:13 AM #13
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 ?USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 03-21-2009, 11:27 PM #14
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 03-22-2009, 10:49 PM #15
ImageIcon myIcon = new ImageIcon();
its not showing up because you don't have any images added.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads
-
getSize() issue - Displaying object in 3 locations in Applet
By furry in forum New To JavaReplies: 4Last Post: 03-21-2009, 11:43 PM -
Setting the DSN
By tim in forum JDBCReplies: 1Last Post: 02-14-2008, 09:55 PM -
New project VFSJFileChooser(browse local and remote locations)
By mrcheeks in forum AWT / SwingReplies: 0Last Post: 01-22-2008, 11:05 AM -
Help with setting up please
By BlitzA in forum New To JavaReplies: 6Last Post: 12-29-2007, 12:54 PM -
Object locations via grid coordinates HELP.
By deadman_uk in forum New To JavaReplies: 4Last Post: 11-18-2007, 08:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks