Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have an ArrayList with values like "abcd#xyz" and "mnop#qrs". I want to convert it into an Array and then split it with # as delimiter and have abcd,mnop in an array and xyz,qrs in another array. I tried the following code:

String dsf[] = new String[al.size()];              
for(int i =0;i<al.size();i++){
  dsf[i] = al.get(i);
}

But it failed saying "Ljava.lang.String;@57ba57ba"

Can you help me please.

share|improve this question
2  
POst the definition of your ArrayList and the complete error message – Guillaume Polet Mar 29 '12 at 16:18
    
How is your al declared? – Aleks G Mar 29 '12 at 16:18
6  
What do you mean by "it failed saying ..."? That looks like the result of printing dsf.toString()... – Jon Skeet Mar 29 '12 at 16:19
    
Do you want an array of arrays of Strings, i.e. a 2 dimensional array? – Adam Mar 29 '12 at 16:20
1  

6 Answers 6

You don't need to reinvent the wheel, here's the toArray() method:

String []dsf = new String[al.size()];
al.toArray(dsf);
share|improve this answer
    
ArrayList<String> a1 = new ArrayList<String>(); for (Entry<String, String> entry : outmap.entrySet()) { if(entry.getKey().indexOf("Data") != -1){ exports.add(entry.getValue()); } } – Shruthi Mar 29 '12 at 16:24
    
There's also this form 'docs.oracle.com/javase/1.5.0/docs/api/java/util/…; for where you want to provide a specificly typed array. – wmorrison365 Mar 29 '12 at 16:29
List<String> list=new ArrayList<String>();
list.add("sravan");
list.add("vasu");
list.add("raki");
String names[]=list.toArray(new String[list.size()])
share|improve this answer

ArrayList to Array

List<String> list = new ArrayList<String>();

list.add("India");
list.add("Switzerland");
list.add("Italy");
list.add("France");

String [] countries = list.toArray(new String[list.size()]);
share|improve this answer
    
"string" should be "String" in the first line – Zsolti Sep 22 at 14:45

What you did with the iteration is not wrong from what I can make of it based on the question. It gives you a valid array of String objects. Like mentioned in another answer it is however easier to use the toArray() method available for the ArrayList object => http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#toArray%28%29

Just a side note. If you would iterate your dsf array properly and print each element on its own you would get valid output. Like this:

for(String str : dsf){
   System.out.println(str);
}

What you probably tried to do was print the complete Array object at once since that would give an object memory address like you got in your question. If you see that kind of output you need to provide a toString() method for the object you're printing.

share|improve this answer
package com.v4common.shared.beans.audittrail;

import java.util.ArrayList;
import java.util.List;

public class test1 {
    public static void main(String arg[]){
        List<String> list = new ArrayList<String>();
        list.add("abcd#xyz");
        list.add("mnop#qrs");

        Object[] s = list.toArray();
        String[] s1= new String[list.size()];
        String[] s2= new String[list.size()];

        for(int i=0;i<s.length;i++){
            if(s[i] instanceof String){
                String temp = (String)s[i];
                if(temp.contains("#")){
                    String[] tempString = temp.split("#");
                    for(int j=0;j<tempString.length;j++) {
                        s1[i] = tempString[0];
                        s2[i] = tempString[1];
                    }

                }
            }   
        }
        System.out.println(s1.length);
        System.out.println(s2.length);
        System.out.println(s1[0]);
        System.out.println(s1[1]);
    }
}
share|improve this answer

Here is the solution for you given scenario -

List<String>ls = new ArrayList<String>();
    ls.add("dfsa#FSDfsd");
    ls.add("dfsdaor#ooiui");
    String[] firstArray = new String[ls.size()];    
 firstArray =ls.toArray(firstArray);
String[] secondArray = new String[ls.size()];
for(int i=0;i<ls.size();i++){
secondArray[i]=firstArray[i].split("#")[0];
firstArray[i]=firstArray[i].split("#")[1];
} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.