Array referencing

A

anders_tung

Hi,

Here is my routine. It is not working. Anyone see the problem?

public void test ()
{

String[][] data_1 = {
{ "AA_1", "Hello world" },
{ "AA_2", "123456789" }
};
String[][] data_2 = {
{ "BB_1", "Bla Bla" },
{ "BB_1", "222222222" },
};
String[][] data_3 = {
{ "CC_1", "Happy New Year" },
{ "CC_1", "333333333" },
};

for(int i=1; i <=3; i++){
// following line is wrong. Anyone know how to do it??
String data_name[][] = "data_"+i;
System_out_println(" first = "+data_name[j][0];
System_out_println(" second = "+data_name[j][1];
}
}

Thank you!

Anders
 
M

Michael Borgwardt

// following line is wrong. Anyone know how to do it??
String data_name[][] = "data_"+i;

What is it *supposed* to do?

You're trying to assing a String to a reference of type String[][],
which is obviously not possible.
 
A

Andrei Kouznetsov

Here is my routine. It is not working. Anyone see the problem?
public void test ()
{

String[][] data_1 = {
{ "AA_1", "Hello world" },
{ "AA_2", "123456789" }
};
String[][] data_2 = {
{ "BB_1", "Bla Bla" },
{ "BB_1", "222222222" },
};
String[][] data_3 = {
{ "CC_1", "Happy New Year" },
{ "CC_1", "333333333" },
};

for(int i=1; i <=3; i++){
// following line is wrong. Anyone know how to do it??
String data_name[][] = "data_"+i;
System_out_println(" first = "+data_name[j][0];
System_out_println(" second = "+data_name[j][1];
}
}

I suppose you are from VisualBasic world...

What you can do is following:

Object [] data = new Object[3];
data[0] = data_1;
data[1] = data_2;
data[2] = data_3;

for(int i=0; i < 3; i++){
String data_name[][] = (String[][]) data;
System_out_println(" first = "+data_name[j][0];
System_out_println(" second = "+data_name[j][1];
}
 
B

Boudewijn Dijkstra

Andrei Kouznetsov said:
Here is my routine. It is not working. Anyone see the problem?

public void test ()
{

String[][] data_1 = {
{ "AA_1", "Hello world" },
{ "AA_2", "123456789" }
};
String[][] data_2 = {
{ "BB_1", "Bla Bla" },
{ "BB_1", "222222222" },
};
String[][] data_3 = {
{ "CC_1", "Happy New Year" },
{ "CC_1", "333333333" },
};

for(int i=1; i <=3; i++){
// following line is wrong. Anyone know how to do it??
String data_name[][] = "data_"+i;
System_out_println(" first = "+data_name[j][0];
System_out_println(" second = "+data_name[j][1];
}
}

I suppose you are from VisualBasic world...

What you can do is following:

Object [] data = new Object[3];
data[0] = data_1;
data[1] = data_2;
data[2] = data_3;

for(int i=0; i < 3; i++){
String data_name[][] = (String[][]) data;
System_out_println(" first = "+data_name[j][0];
System_out_println(" second = "+data_name[j][1];
}


Please place your brackets consistently, preferably directly behind the
element type when declaring. That goes for the both of you.
 
A

Andrei Kouznetsov

Please place your brackets consistently, preferably directly behind the
element type when declaring. That goes for the both of you.

I just copy/paste his code, I always put brackets in the middle.
 
R

Ricardo

Hi,

Here is my routine. It is not working. Anyone see the problem?

public void test ()
{

String[][] data_1 = {
{ "AA_1", "Hello world" },
{ "AA_2", "123456789" }
};
String[][] data_2 = {
{ "BB_1", "Bla Bla" },
{ "BB_1", "222222222" },
};
String[][] data_3 = {
{ "CC_1", "Happy New Year" },
{ "CC_1", "333333333" },
};

for(int i=1; i <=3; i++){
// following line is wrong. Anyone know how to do it??
String data_name[][] = "data_"+i;
System_out_println(" first = "+data_name[j][0];
System_out_println(" second = "+data_name[j][1];
}
}

It seems you want to reference data_1 array when you say "data_"+i.
You cannot do that. Actually you put an String value(/"data_"+i/) in an
array, which is wrong.
What do want to do would be:


public void test ()
{
String[][][] data = {
{
{"AA_1", "Hello world"},
{"AA_2", "123456789"}
},
{
{"BB_1", "Bla Bla"},
{"BB_1", "222222222"}
},
{
{"CC_1", "Happy New Year"},
{"CC_1", "333333333"}
}};


for(int i=0; i <=1; i++){
System_out_println(" first = "+data[j][0];
System_out_println(" second = "+data[j][1];
}
}
 
N

Nigel Wade

Hi,

Here is my routine. It is not working. Anyone see the problem?

public void test ()
{

String[][] data_1 = {
{ "AA_1", "Hello world" },
{ "AA_2", "123456789" }
};
String[][] data_2 = {
{ "BB_1", "Bla Bla" },
{ "BB_1", "222222222" },
};
String[][] data_3 = {
{ "CC_1", "Happy New Year" },
{ "CC_1", "333333333" },
};

for(int i=1; i <=3; i++){
// following line is wrong. Anyone know how to do it??
String data_name[][] = "data_"+i;
System_out_println(" first = "+data_name[j][0];
System_out_println(" second = "+data_name[j][1];
}
}

Thank you!

Anders

Off the top of my head, one way to do this is to use a HashMap:

HashMap dataMap = new HashMap();
....
dataMap.put("data_1",data_1);
dataMap.put("data_2",data_2);
dataMap.put("data_3",data_3);
for(int i=1; i <=3; i++){
String[][] data_name = (String[][]) dataMap.get("data_"+i);

This isn't a general solution to this type of problem, but for this specific
case it gets the job done, and it's pretty simple.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top