Converting ArrayList to Array in java

前端 未结 10 1872
野的像风
野的像风 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: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.

提交回复
热议问题