8

for example I want to do this:

String[] arraynames = new String[2];
arraynames[0] = "fruits";
arraynames[1] = "cars";

and now I don't know how to do this

String[] arraynames[0] = new String[100]; // ??????

so that I create a String array called fruits with 100 cells... I know this doesn't work but is there someway to do this?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Johny
  • 319
  • 2
  • 5
  • 10
  • are you saying you want an array of arrays or just an array named fruits? – johnny Nov 24 '10 at 21:50
  • He wants to store some strings in an Array, and then use the data of the array and name variables after that. Something which AFAIK cannot happen –  Nov 24 '10 at 21:55

7 Answers7

18

Use an HashMap

Example:

HashMap<String,String[]> arraynames = new HashMap<String,String[]>();
arraynames.put("fruits", new String[1000]);

// then simply access it with
arraynames.get("fruits")[0] = "fruit 1";

However, may I suggest you replace arrays with ArrayList ?

HashMap<String,ArrayList<String>> arraynames = new HashMap<String,ArrayList<String>>();
arraynames.put("fruits", new ArrayList<String>());

// then simply access it with
arraynames.get("fruits").add("fruit 1");

** EDIT **

To have an array of float values instead of strings

HashMap<String,ArrayList<Float>> arraynames = new HashMap<String,ArrayList<Float>>();
arraynames.put("fruits", new ArrayList<Float>());

// then simply access it with
arraynames.get("fruits").add(3.1415f);
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
1

So, you are looking for a doubly indexed array?

Something like:

String[][] arraynames = String[2][100];

You have created an array of 2 arrays that contain 100 String elements each in this case.

aperkins
  • 12,914
  • 4
  • 29
  • 34
0

I hope I get you right, you could something like this:

ArrayList arraynames = new ArrayList();

arraynames.add("fruits"); 
arraynames.add("cars");

arraynames.set(0, new ArrayList(100) );
stacker
  • 68,052
  • 28
  • 140
  • 210
0

I am not quite clear on the second line of code here. You said you are trying to create a string array named fruits. This should suffice if I understood this right.

String [] fruits = new String[100]; 
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
  • I know this... The thing is I want to take the names of the arrays from another array, and they are like 100 of them... – Johny Nov 24 '10 at 21:57
  • I am not sure about taking names of arrays from another array. However, if you follow Yanick Rochon's approach, you can use the array name as the key in the Map and then the array itself is the value that you store data in. – CoolBeans Nov 24 '10 at 22:02
0

you should use a bi-dimensional array like this:

String[][] myData = new String[2][100];

now myData is an array with 2 elements, both of them are arrays of "100 cells", then you can use these 2 arrays as follows:

String[] fruits = myData[0];
String[] cars = myData[1];
fruits[0] = "peach";
cars[0] = "mustang";
RRoman
  • 741
  • 9
  • 19
0

Using Guava library:

ListMultimap<String,String> mappedItems = ArrayListMultimap.create();
mappedItems.put("Fruits","Apple");
mappedItems.put("Fruits","Orange");
mappedItems.put("Fruits","Banana");
mappedItems.put("Cars", "BMW");
mappedItems.put("Cars","Ferrari");
System.out.println(mappedItems);

Output:

{Cars=[BMW, Ferrari], Fruits=[Apple, Orange, Banana]}

Emil
  • 13,577
  • 18
  • 69
  • 108
0

You can use one of the following :

String[][] arraynames = String[2][100];
Map<String, String[]> namesMap = new HashMap<String, String[]>();
List<List<String>> names = new ArrayList<ArrayList<String>()>>();
fastcodejava
  • 39,895
  • 28
  • 133
  • 186