Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 526
  • Last Modified:

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
0
gudii9
Asked:
gudii9
  • 5
  • 3
  • 3
2 Solutions
 
CEHJCommented:
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

0
 
gudii9Author Commented:
Howe to write without Java 1.5 generics
0
 
CEHJCommented:
Just delete everything in angle brackets
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
zzynxSoftware engineerCommented:
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];
0
 
gudii9Author Commented:
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
0
 
zzynxSoftware engineerCommented:
When I run this code

    public static void main(String[] args) {
        String[][] aastr = {{"hello", "world"},{"Goodbye", "planet"}};
        String yourHelloString = aastr[0][0];
        String yourWorldString = aastr[0][1];
        String yourTGoodbyeString = aastr[1][0];
        String yourPlanetString = aastr[1][1];
    }

Open in new window


I have no errors.
Maybe it's because your array was named "asstr" and my code used "aastr"?
Of course, you should align those names.
0
 
CEHJCommented:
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

0
 
gudii9Author Commented:
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

0
 
CEHJCommented:
I can only guess at the arrangement of the data if the array is one-dimensional. Try
    static List getRows(String[] data) {
        List result = new ArrayList(data.length);

        for (int i = 0; i < data.length; i += 2) {
            result.add(Arrays.asList(data[i], data[i + 1]));
        }

        return result;
    }

Open in new window

0
 
zzynxSoftware engineerCommented:
Thanx 4 axxepting
0
 
CEHJCommented:
:)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now