Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multi-Dimensional Arrays

 
Alan Arkwell
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to multi-dimensional arrays and I am trying to create one that accepts a user input (name and home town).I am thinking that a MD array would store the two inputs alongside each other.Here is my code:
import javax.swing.JOptionPane;
public class AlansProg
{
public static void main(String[]args)
{

String Info[][] = new String[4][2];
for(int row=0;row<4;row++)
{

Info[row] = JOptionPane.showInputDialog("enter name");

}

for(int col=0;col<2;col++)

{

Info[col] = JOptionPane.showInputDialog("enter Home Town");

}

}
}
I am getting this error message :
incompatible types
found:java.lang.string
required:java.lang.String[]
Info[row] = JOptionPane.showInputDialog("enter name");
^
Am I right to think this can be done,or do I need to create seperate arrays?
Cheers...Alan.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you're using a multidimensional array (an array of arrays), Info[row] is expecting another array. There's nothing wrong with multidimensional arrays, per se, but you should look at encapsulating related information in an object, then storing the object(s) in a Collection.

Object-Oriented Programming Concepts
Object-oriented language basics
Don't Fear the OOP

Java� Collections Framework
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are using a multi-dimensional array, you need to provide multiple indices to access a single element. As you have it now, Info[row] gives you the whole row at that index. If you want to access a single String, you need to do something like Info[row][col] to get the individual element.

However, as noted above, this doesn't seem like a poper use for multidimentional arrays. Typically, an array (whether 1D, 2D, 3D, or more) should be used to hold similar data. In this case, each data item, while related, are not similar. It seems to me that a custom object is appropriate here to aggregate the data for an individaul. Then you can create a single-dimensional array (or a Collection) to hold many of these objects.

Layne
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We kinda beat you up on your example, but the answer about using two indexes was in there. While the custom object is a nicer solution, you may bump into arrays used this way. You might use some constants to document what's in teh columns:
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Along with the suggestion of creating objects and then storing them in the array, you could also look at using HashMap.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic