2 回答
TA贡献1798条经验 获得超7个赞
如果要将元素存储到数组中,可以List先将其放入List数组中,然后通过List.toArray以下方式将其转换为数组:
LinkedList<String> linkedList = new LinkedList<>();
for (String res : listOne) {
if (res.startsWith("a") || res.startsWith("g")) {
System.out.println("--> " + res);
} else {
System.out.println("** " + res);
linkedList.add(res);
}
}
String[] result1 = new String[linkedList.size()];
linkedList.toArray(result1);
TA贡献1836条经验 获得超3个赞
Please find the code for storing the element in String array using index i;
package com.test.stackoverflow;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TestClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
display();
}
public static void display() {
String[] inpArray = { "apple", "orange", "grapes" };
List<String> listOne = new ArrayList<String>(Arrays.asList(inpArray));
List<String> listTwo = new ArrayList<String>();
listTwo.add("melon");
listTwo.add("apple");
listTwo.add("mango");
String[] result1 = new String[listOne.size()];
int i =0;
for (String res : listOne) {
if (res.startsWith("a") || res.startsWith("g")) {
System.out.println("--> " + res);
} else {
System.out.println("** " + res);
result1[i++] = res;
}
}
for(String val : result1){
System.out.println("Value"+val);
}
}
}
添加回答
举报