Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

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.deepToString(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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You probably need something like:

    static List<List<String>> getRows(String[][] data) {
	List<List<String>> result = new ArrayList<List<String>>(data.length);
	for(String[] row : data) {
	    result.add(Arrays.asList(row));
	}
	return result;
    }

Open in new window

Avatar of gudii9

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];
Avatar of gudii9

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
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;
    }

Open in new window

Avatar of gudii9

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]

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx 4 axxepting
:)