gudii9
asked on
fetch from string array into collection
I have following program
import java.util.Arrays;
public class TwoDArrayRead {
public static void main( String [] args ) {
String[][] aastr = {{"hello", "world"},{"Goodbye", "planet"}};
System.out.println(Arrays. deepToStri ng(aastr)) ;
}
}
It is giving output as
[[hello, world],
[Goodbye, planet]]
I want to take out
hello and world
similarly
Goodbye, planet
and put it into some collection etc
so that later i can loop through and query database using
hello as 'username' and world as 'password'
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
import java.util.Arrays;
public class TwoDArrayRead {
public static void main( String [] args ) {
String[][] aastr = {{"hello", "world"},{"Goodbye", "planet"}};
System.out.println(Arrays.
}
}
It is giving output as
[[hello, world],
[Goodbye, planet]]
I want to take out
hello and world
similarly
Goodbye, planet
and put it into some collection etc
so that later i can loop through and query database using
hello as 'username' and world as 'password'
Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
ASKER
Howe to write without Java 1.5 generics
Just delete everything in angle brackets
When you have
>> String[][] aastr = {{"hello", "world"},{"Goodbye", "planet"}};
then
String[] one = asstr[0]; // String array containing {"hello", "world"}
String[] two = asstr[1]; // String array containing {"Goodbye", "planet"}
String yourHelloString = one[0];
String yourWorldString = one[1];
String yourTGoodbyeString = two[0];
String yourPlanetString = two[1];
or
String yourHelloString = asstr[0][0];
String yourWorldString = asstr[0][1];
String yourTGoodbyeString = asstr[1][0];
String yourPlanetString = asstr[1][1];
>> String[][] aastr = {{"hello", "world"},{"Goodbye", "planet"}};
then
String[] one = asstr[0]; // String array containing {"hello", "world"}
String[] two = asstr[1]; // String array containing {"Goodbye", "planet"}
String yourHelloString = one[0];
String yourWorldString = one[1];
String yourTGoodbyeString = two[0];
String yourPlanetString = two[1];
or
String yourHelloString = asstr[0][0];
String yourWorldString = asstr[0][1];
String yourTGoodbyeString = asstr[1][0];
String yourPlanetString = asstr[1][1];
ASKER
String yourHelloString = asstr[0][0];
String yourWorldString = asstr[0][1];
String yourTGoodbyeString = asstr[1][0];
String yourPlanetString = asstr[1][1];
when I write like above getting error message like
The type of expression must be an array type but it resolved to string.
Please advise on how to resolve above error message
String yourWorldString = asstr[0][1];
String yourTGoodbyeString = asstr[1][0];
String yourPlanetString = asstr[1][1];
when I write like above getting error message like
The type of expression must be an array type but it resolved to string.
Please advise on how to resolve above error message
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just delete everything in angle brackets
Actually that's not all that's needed sorry - forgot about for loop:
static List getRows(String[][] data) {
List result = new ArrayList(data.length);
for(int i = 0;i<data.length;i++) {
result.add(Arrays.asList(row[i]));
}
return result;
}
ASKER
it worked perfect as below
[quote]import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TwoDArrayRead {
public static void main( String [] args ) {
String[][] data = {{"hello", "world"},{"Goodbye", "planet"}};
System.out.println(Arrays.deepToString(data));
getRows(data);
}
static List getRows(String[][] data) {
List result = new ArrayList(data.length);
for(int i = 0;i<data.length;i++) {
result.add(Arrays.asList(data[i]));
}
return result;
}
}
[/quote]
if
>>>String[][] data = {{"hello", "world"},{"Goodbye", "planet"}};
'data' is 1 dimentional instead of 2 dimentional how do I modify the program to read the values similar to above. Please advise [/i]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanx 4 axxepting
:)
Open in new window