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:
我有一个ArrayList,值为“abcd#xyz”和“mnop#qrs”。我想把它转换成一个数组,然后用#作为分隔符将它分割成abcd,mnop作为数组,xyz,qrs作为另一个数组。我尝试了以下代码:
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"
但它没有说“Ljava.lang.String;@57ba57ba”
Can you help me please.
你能帮帮我吗?
10 个解决方案
#1
110
You don't need to reinvent the wheel, here's the toArray()
method:
您不需要重新创建*,下面是toArray()方法:
String []dsf = new String[al.size()];
al.toArray(dsf);
#2
50
List<String> list=new ArrayList<String>();
list.add("sravan");
list.add("vasu");
list.add("raki");
String names[]=list.toArray(new String[list.size()])
#3
6
List<String> list=new ArrayList<String>();
list.add("sravan");
list.add("vasu");
list.add("raki");
String names[]=list.toArray(new String[0]);
if you see the last line (new String[0]), you don't have to give the size, there are time when we don't know the length of the list, so to start with giving it as 0 , the constructed array will resize.
如果你看到最后一行(新的字符串[0]),你不需要给它大小,有一段时间我们不知道列表的长度,所以要以0开始,构建的数组将会重新调整大小。
#4
2
String[] values = new String[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
values[i] = arrayList.get(i).type;
}
#5
1
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
你对迭代所做的一切,从我基于这个问题所能得出的结论来看,并没有错。它给你一个有效的字符串对象数组。就像在另一个答案中提到的那样,对于ArrayList对象=> http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#toArray%28%29来说,使用toArray()方法更容易
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:
边注。如果您要正确地迭代您的dsf数组并打印每个元素,您将得到有效的输出。是这样的:
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.
您可能想要做的是立即打印完整的数组对象,因为这将提供一个对象内存地址,就像您在问题中得到的那样。如果看到这种输出,您需要为正在打印的对象提供toString()方法。
#6
1
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]);
}
}
#7
1
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];
}
#8
1
import java.util.*;
public class arrayList {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<String > x=new ArrayList<>();
//inserting element
x.add(sc.next());
x.add(sc.next());
x.add(sc.next());
x.add(sc.next());
x.add(sc.next());
//to show element
System.out.println(x);
//converting arraylist to stringarray
String[]a=x.toArray(new String[x.size()]);
for(String s:a)
System.out.print(s+" ");
}
}
#9
1
This is the right answer you want and this solution i have run my self on netbeans
这是你想要的正确答案,这是我在netbeans上运行的解决方案
ArrayList a=new ArrayList();
a.add(1);
a.add(3);
a.add(4);
a.add(5);
a.add(8);
a.add(12);
int b[]= new int [6];
Integer m[] = new Integer[a.size()];//***Very important conversion to array*****
m=(Integer[]) a.toArray(m);
for(int i=0;i<a.size();i++)
{
b[i]=m[i];
System.out.println(b[i]);
}
System.out.println(a.size());
#10
0
This can be done using stream:
这可以使用流来完成:
List<String> stringList = Arrays.asList("abc#bcd", "mno#pqr");
List<String[]> objects = stringList.stream()
.map(s -> s.split("#"))
.collect(Collectors.toList());
The return value would be arrays of split string. This avoids converting the arraylist to an array and performing the operation.
返回值将是分割字符串的数组。这避免了将arraylist转换为数组并执行操作。
#1
110
You don't need to reinvent the wheel, here's the toArray()
method:
您不需要重新创建*,下面是toArray()方法:
String []dsf = new String[al.size()];
al.toArray(dsf);
#2
50
List<String> list=new ArrayList<String>();
list.add("sravan");
list.add("vasu");
list.add("raki");
String names[]=list.toArray(new String[list.size()])
#3
6
List<String> list=new ArrayList<String>();
list.add("sravan");
list.add("vasu");
list.add("raki");
String names[]=list.toArray(new String[0]);
if you see the last line (new String[0]), you don't have to give the size, there are time when we don't know the length of the list, so to start with giving it as 0 , the constructed array will resize.
如果你看到最后一行(新的字符串[0]),你不需要给它大小,有一段时间我们不知道列表的长度,所以要以0开始,构建的数组将会重新调整大小。
#4
2
String[] values = new String[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
values[i] = arrayList.get(i).type;
}
#5
1
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
你对迭代所做的一切,从我基于这个问题所能得出的结论来看,并没有错。它给你一个有效的字符串对象数组。就像在另一个答案中提到的那样,对于ArrayList对象=> http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#toArray%28%29来说,使用toArray()方法更容易
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:
边注。如果您要正确地迭代您的dsf数组并打印每个元素,您将得到有效的输出。是这样的:
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.
您可能想要做的是立即打印完整的数组对象,因为这将提供一个对象内存地址,就像您在问题中得到的那样。如果看到这种输出,您需要为正在打印的对象提供toString()方法。
#6
1
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]);
}
}
#7
1
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];
}
#8
1
import java.util.*;
public class arrayList {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<String > x=new ArrayList<>();
//inserting element
x.add(sc.next());
x.add(sc.next());
x.add(sc.next());
x.add(sc.next());
x.add(sc.next());
//to show element
System.out.println(x);
//converting arraylist to stringarray
String[]a=x.toArray(new String[x.size()]);
for(String s:a)
System.out.print(s+" ");
}
}
#9
1
This is the right answer you want and this solution i have run my self on netbeans
这是你想要的正确答案,这是我在netbeans上运行的解决方案
ArrayList a=new ArrayList();
a.add(1);
a.add(3);
a.add(4);
a.add(5);
a.add(8);
a.add(12);
int b[]= new int [6];
Integer m[] = new Integer[a.size()];//***Very important conversion to array*****
m=(Integer[]) a.toArray(m);
for(int i=0;i<a.size();i++)
{
b[i]=m[i];
System.out.println(b[i]);
}
System.out.println(a.size());
#10
0
This can be done using stream:
这可以使用流来完成:
List<String> stringList = Arrays.asList("abc#bcd", "mno#pqr");
List<String[]> objects = stringList.stream()
.map(s -> s.split("#"))
.collect(Collectors.toList());
The return value would be arrays of split string. This avoids converting the arraylist to an array and performing the operation.
返回值将是分割字符串的数组。这避免了将arraylist转换为数组并执行操作。