[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String array to Arralist and vice versa conversion

Posted on 2011-05-03
8
Medium Priority
?
389 Views
Last Modified: 2012-05-11
How do I convert

String[]  to  ArrayList  

and  

ArrayList  to String[]

Reason :
I have a VO field  String[]  .  I need to poulate this field  in a while loop .  So, I need to know the above two conversions.

 
0
Comment
Question by:cofactor
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35510700
See ArrayList.toArray

and for the reverse, all you need to do is create a List of the same size and each array element to it.

0
 

Author Comment

by:cofactor
ID: 35511208
Not clear.
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 35511252
The code using strings:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package convertarray;

import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @author darren
 */
public class Convert {

    private ArrayList<String> al;
    private ArrayList<String> al2;
    private String ia[];

    public void createArrList() {

        al = new ArrayList<String>();
        al.add("a");
        al.add("b");
        al.add("c");
        al.add("d");

        System.out.println("Contents of al: " + al);

        ia = new String[al.size()];
        ia = al.toArray(ia);

        String sum = "";

        for (String i : ia) {
            sum += i + ",";
        }

        System.out.println("Strings are: " + sum);
        //convert array to Arraylist
        
        al2 = new ArrayList<String>();
        al2.addAll(Arrays.asList(ia));
        System.out.println("From arrayaylist:");
        for (String st : al2) {
            System.out.println(st);
        } 
    }
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:darren-w-
ID: 35511276
I've taken the given example, converted it to use Strings, it takes a creates a string arraylist converts it to a string  array  then converts the array to a arraylist.

Darren
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 35511524
e.g.
import java.io.*;

import java.util.*;


public class Arr {
    public static void main(String[] args) {
	// To List
        String[] a = { "a", "b", "c" };
        List<String> list = getList(a);
        System.out.println(list);

	// From List
	String[] copy = list.toArray(new String[list.size()]);
	System.out.println(Arrays.toString(copy));
    }

    public static List<String> getList(String[] a) {
        List<String> result = new ArrayList<String>(a.length);

        for (String s : a) {
            result.add(s);
        }

        return result;
    }
}

Open in new window

0
 

Author Closing Comment

by:cofactor
ID: 35511710
Excellent
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 35511753
I believe that Arrays.asList(Array) circumnavigates using the list method above (line 43 in my example)? No to happy about the point allocation here sorry?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35512167
darren is right. Actually it's even simpler (as long as mutation doesn't occur). Happy to split points btw
List<String> converted = Arrays.asList(ia);

Open in new window

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question