英文:
How do I convert an array of strings into append-able list?
问题
I have parsed through a CSV file and stored it as an array of strings. I'd like to add an item to each sub-array if certain conditions have been met, as shown below. However, I am not able to use the .add()
method and get a "Cannot resolve method" message in my IDE. To try to get around this, I attempted to create a new ArrayList in which I placed the contents of the string array, but the problem persisted. How do I make it so that I can add an item to each sublist?
import java.util.List;
import java.util.ArrayList;
public class Application {
/**
* Main entry of the application.
*
* @param args This should be empty
*/
public static void main(final String[] args) {
String csvFile = "/IUCNListV2.csv";
List<String[]> listAnimal =
ReadCSV.readFileAndParseSkipFirstline(Application.class.getResourceAsStream(csvFile));
List<String[]> list2 = new ArrayList<>();
for (String [] text : listAnimal) {
list2.add(text);
}
for(String[] subList: list2)
if (null != subList && subList[4].equals("x") &&
subList[5].equals("y")) {
subList.add("z");
}
}
}
英文:
I have parsed through a CSV file and stored it as an array of strings. I'd like to add an item to each sub-array if certain conditions have been met, as shown below. However, I am not able to use the .add()
method and get a "Cannot resolve method" message in my IDE. To try to get around this, I attempted to create a new arraylist in which I placed the contents of the string array, but the problem persisted. How do I make it so that I can add an item to each sublist?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
import java.util.List;
import java.util.ArrayList;
public class Application {
/**
* Main entry of the application.
*
* @param args This should be empty
*/
public static void main(final String[] args) {
String csvFile = "/IUCNListV2.csv";
List<String[]> listAnimal =
ReadCSV.readFileAndParseSkipFirstline(Application.class.getResourceAsStream(csvFile));
List<String[]> list2 = new ArrayList<>();
for (String [] text : listAnimal) {
list2.add(text);
}
for(String[] subList: list2)
if (null != subList && subList[4].equals("x") &&
subList[5].equals("y")) {
subList.add("z");
}
}
}
<!-- end snippet -->
答案1
得分: 0
To convert an array of String to ArrayList simply use this:
String[] x = {"example1", "example2"};
ArrayList<String> list = new ArrayList<>(Arrays.asList(x));
英文:
To convert an array of String to ArrayList simply use this
String[] x = {"example1","example2"};
ArrayList<String> list = new ArrayList<>(Arrays.asList(x));
答案2
得分: 0
你不能在数组上使用add方法,因为add方法在List接口中,但你可以像这样在其他列表中使用List
public class Application {
/**
* 应用程序的主入口。
*
* @param args 这应该是空的
*/
public static void main(final String[] args) {
String csvFile = "/IUCNListV2.csv";
List<List<String>> listAnimal =
ReadCSV.readFileAndParseSkipFirstline(Application.class.getResourceAsStream(csvFile));
List<List<String>> list2 = new ArrayList<>();
list2.addAll(listAnimal);
for(List<String> subList: list2)
if (null != subList && subList.get(4).equals("x") &&
subList.get(5).equals("y")) {
subList.add("z");
}
}
}
英文:
You can't use the add method on arrays, because the add method is in the List interface, but you can use List<String> objects in your other list like this, for this solution:
public class Application {
/**
* Main entry of the application.
*
* @param args This should be empty
*/
public static void main(final String[] args) {
String csvFile = "/IUCNListV2.csv";
List<List<String>> listAnimal =
ReadCSV.readFileAndParseSkipFirstline(Application.class.getResourceAsStream(csvFile));
List<List<String>> list2 = new ArrayList<>();
list2.addAll(listAnimal);
for(List<String> subList: list2)
if (null != subList && subList.get(4).equals("x") &&
subList.get(5).equals("y")) {
subList.add("z");
}
}
}
答案3
得分: 0
你不能直接在字符串数组中添加元素。你可以按照以下步骤操作:
- 你可以从字符串数组创建一个列表
- 在列表中添加元素
- 然后再将列表转换回数组
- 更新外部列表
或者首先从 List<String[]>
创建一个 List<List<String>>
,然后在内部列表中添加元素。
英文:
You can't add element in string array directly. You can follow those steps
- You can create list from string array
- Add the element in list
- Again convert list to array
- Update the outer list
for (int i = 0; i < list2.size(); i++) {
String[] subList = list2.get(i);
if (null != subList && subList[4].equals("x") &&
subList[5].equals("y")) {
ArrayList<String> list = new ArrayList<>(Arrays.asList(subList)); // create a list
list.add("z"); // add in list
list2.set(i, list.toArray(new String[0])) // create array from list and update parent list
}
}
Or create a List<List<String>>
from List<String[]>
first then add in inner list
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论