File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Multidimensional Array Nightmare! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Multidimensional Array Nightmare!" Watch "Multidimensional Array Nightmare!" New topic
Author

Multidimensional Array Nightmare!

Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
Hello, i'm having issues trying to sort out a multidimensional array for a JTable. However, before i pass my array to a JTable, i'd like to work out how to set it up properly and then System.out.println it on the command line so i can have a rough idea what the table will look like. Here's the code i've been playing with.

1. To set up the array (assuming colum size will always be 3, whilst row size depending on whatever database returns):

Object[][] data = new Object[rowSize][3];

for (int i=0; i<data.length; i++)
{
data[i][0] = theString;
data[i][1] = theInteger;
data[i][2] = theDouble;
}

2. To display it on the command line:

for (int row = 0; row < data.length; row++)
{
for (int col = 0; col < data.length[row].length; col++)
{
System.out.println(data[row][col] + "\t");
}
System.out.println();
}

When i try and run this i get a crazy response with the array not printing out right and simple repeating the code.

I'd like to see on the screen something like:

theString1 theInteger1 theDouble1
theString2 theInteger2 theDouble2
theString3 theInteger3 theDouble3
etc etc

If any one has any suggestions i would be most grateful i've been messing with this for days

Cheers!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24059
    
  13

Well, you're reasonably close. First, the printing loop wouldn't actually compile --

data.length[row].length

isn't valdid syntax. I assume you're really saying "data[row].length".

Second, if you want all the entries for a row to be on the same line, you should not use "println" to print them, but rather "print". You're already printing a tab after each item, so they'll be separated on the page.

And finally, as shown, the loop to fill the array (which I recognize as an example I showed you or someone else earlier this week) would put the same data into each row of the array; you need more code in that loop to set new values for those variables each time around, right? Otherwise you'd see exactly what you're seeing: repeated data.


[Jess in Action][AskingGoodQuestions]
Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
Ernest THANK YOU!

My code was updating the query in the loop - i just kept it simple for here. However the bloody println was causing all the havoc! I feel quite silly now. Thanks again!!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Multidimensional Array Nightmare!
 
Similar Threads
JTable
jtable
Multidimensional Arrays
Right Align numbers in Jtable
Multidimensional ArrayList