• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to initialize two dimensional array in for loop  RSS feed

 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am failing to initialize the data Object array in for loop..i think there is some syntax error. Can anybody please help me..

Regards
Sudhakar
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only use an array initialiser in the declaration statement. Anywhere else you have to write

= new Foo[]{ . . . };
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, why are you initialising the same array 10 times?
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, why are you initialising the same array 10 times?


That was unintentional

Can you please explain how to add data to the data object each time in the loop.I am trying to do like this but no success..


Many thanks for your reply..

Regards
Sudhakar
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use data object in the JTable for displaying data.In each iteration of the loop i want to add diiferent data and atlast pass this as argument to the JTable.

Regards
Sudhakar
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you write a for loop to iterate through an ordinary array with the .length field?
Write down an ordinary array on paper, maybe int[] array = new int[]{1, 2, 3, 4}.
Now write down a 2-subscript array on paper, maybe int[][] numbers = new int[][]{{1, 2, 3}, {4, 5}, {}, {6, 7, 8, 9, 10}, {11}}. That is an array of arrays.
Now write down each element of the outer array, numbers[0], numbers[1], etc, on your paper.
Now you see you will have to initialise numbers to new int[9970][] then you can iterate through numbers and initialise numbers[0], numbers[1], numbers[2] etc etc. Work out what you would have to initialise numbers[0] to . . .
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your second suggestion was a great improvement on the first, but you are not there yet.
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now i ued the above statement but i am getting error as incompatible types.

Plese help..

Regards
Sudhakar
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it..i got it..



This is working.

But when i passed this data to the JTable constructor i am getting Arrayindex out of bounds exception.

Needed help again..

Regards
Sudhakar
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have correctly created a 2-subscript array Now write it out on paper, and work out its size. What is the size of "data" and what is the size of "data[0]?" What size array does your Table require?
And surely you mean {name, start, length} rather than {String name, int start, int length}? That initialisation will only work with versions of Java after auto-boxing was introduced.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!