Converting ArrayList to Array in java

前端 未结 10 1835
野的像风
野的像风 2020-12-04 18:44

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 a

相关标签:
10条回答
  • 2020-12-04 19:17
    String[] values = new String[arrayList.size()];
            for (int i = 0; i < arrayList.size(); i++) {
                values[i] = arrayList.get(i).type;
            }
    
    0 讨论(0)
  • 2020-12-04 19:18
    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]);
        }
    }
    
    0 讨论(0)
  • 2020-12-04 19:20

    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.

    0 讨论(0)
  • 2020-12-04 19:22

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

    String []dsf = new String[al.size()];
    al.toArray(dsf);
    
    0 讨论(0)
  • 2020-12-04 19:28
    List<String> list=new ArrayList<String>();
    list.add("sravan");
    list.add("vasu");
    list.add("raki");
    String names[]=list.toArray(new String[list.size()])
    
    0 讨论(0)
  • 2020-12-04 19:29

    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];
    } 
    
    0 讨论(0)
提交回复
热议问题