将字符串数组转换为可附加的列表。

huangapple go评论29阅读模式
英文:

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 = &quot;/IUCNListV2.csv&quot;;
        List&lt;String[]&gt; listAnimal =
        ReadCSV.readFileAndParseSkipFirstline(Application.class.getResourceAsStream(csvFile));


        List&lt;String[]&gt; list2 = new ArrayList&lt;&gt;();
        for (String [] text : listAnimal) {
            list2.add(text);
        }
        
        for(String[] subList: list2)
            if (null != subList &amp;&amp; subList[4].equals(&quot;x&quot;) &amp;&amp;
                    subList[5].equals(&quot;y&quot;)) {
                subList.add(&quot;z&quot;);
            }
    }
}

<!-- 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 = {&quot;example1&quot;,&quot;example2&quot;};
ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;(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 = &quot;/IUCNListV2.csv&quot;;
        List&lt;List&lt;String&gt;&gt; listAnimal =
                ReadCSV.readFileAndParseSkipFirstline(Application.class.getResourceAsStream(csvFile));


        List&lt;List&lt;String&gt;&gt; list2 = new ArrayList&lt;&gt;();
        list2.addAll(listAnimal);

        for(List&lt;String&gt; subList: list2)
            if (null != subList &amp;&amp; subList.get(4).equals(&quot;x&quot;) &amp;&amp;
                    subList.get(5).equals(&quot;y&quot;)) {
                subList.add(&quot;z&quot;);
            }
    }
}

答案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 &lt; list2.size(); i++) {
    String[] subList = list2.get(i);
    if (null != subList &amp;&amp; subList[4].equals(&quot;x&quot;) &amp;&amp;
                    subList[5].equals(&quot;y&quot;)) {
        ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;(Arrays.asList(subList)); // create a list
        list.add(&quot;z&quot;); // add in list
        list2.set(i, list.toArray(new String[0])) // create array from list and update parent list
    }
}

Or create a List&lt;List&lt;String&gt;&gt; from List&lt;String[]&gt; first then add in inner list

huangapple
  • 本文由 发表于 2020年7月28日 00:29:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63119502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定