• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multidimensional Array Nightmare!

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic