How do I convert an array of strings into append-able list?

howmuch4aG :

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");
            }
    }
}

User - Upvote don't say Thanks :

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

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert java.sql.Array to List<MyCustomEnum>?

How do I convert a C# List<string[]> to a Javascript array?

How do I convert a Python list into a C array by using ctypes?

How do I convert an array of strings to a array of unique values?

How do I convert a Go array of strings to a C array of strings?

How do I convert an object array to an object list

How do I convert an array list to hash separated string?

How to convert a list of strings into a numeric numpy array?

How do I convert an array of strings to an object property in javascript?

how do I convert an object to an array list in JS

How do I convert strings in an array to numbers using forEach?

How do I append a lambda to a list in python?

How can I convert a list of strings to an array of float numbers?

How do I convert a list of strings to a proper sentence

How do I convert a list of strings to a unicode value?

How do I append a list to an empty numpy array

How do I convert a named list to an array of objects

How do I append data to the list in loop?

In Typescript, how do I convert a string of strings as an array of strings?

How do I convert a list of strings to a list of dictionaries?

How do I convert an array or a list into a hashref?

How do I convert a list of strings to integers in Python

How do I correctly convert strings to ints and input them into a list?

in python how do i convert a list of strings to pandas data frame

How to convert list of enums into array of strings?

How do I append a string to an array of strings within a loop in C?

In Powershell, how do I convert an array of PSObjects to an array of Strings?

How do I convert specific strings into specific number in an array?

How do I convert the contents of the array from strings into non-strings?