Results 1 to 2 of 2
  1. #1
    mars123 is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default Arraylist to a 2- dimension array conversion

    I have an arraylist which needs to be converted to a two dimenional array.

    Eg:

    Arraylist contains:
    ------------------
    abc
    def
    ghi
    jkl

    Resultant Array should contain elements in the below fashion:
    -----------------------
    (abc,def)
    (ghi,jkl)

    Thanks in advance.

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    		ArrayList<String> ar = new ArrayList<String>();
    		ar.add("abc");
    		ar.add("def");
    		ar.add("ghi");
    		ar.add("jkl");
    		
    		// error handling, if the array size of not even.
    		if (ar.size() &#37; 2 != 0) {
    			System.out.println("ArrayList cannot have odd number of values if to be converted to String[][]");
    			return;
    		}
    		
    		String[][] ray = new String[ar.size()/2][2];
    		
    		int c = 0;
    		for (int i = 0; i < ar.size(); i = i+2) {
    			ray[c][0] = ar.get(i);
    			ray[c][1] = ar.get(i+1);
    			c++;
    		}

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  2. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 04:59 PM
  3. Converting ArrayList to Array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 11:41 AM
  4. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 01:46 AM
  5. Problem in TIFF Conversion
    By lakshmipriyam in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2007, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •