0

I can't seem to figure out how to replace an array list of strings.

  ArrayList<String[]> Records

So within my for loop i want to replace a record can I keep getting this error.

The method set(int, String[]) in the type ArrayList is not applicable for the arguments (int, String)

        for (int i = 0; i < Records.size(); i++) {
            for (int j = 0; j < 2; j++) {
                if (j == 0) {
                    if(!validateRecords(Records.get(i)[j].toString()))
                    {
                        Logging.info("Records could not be parsed " + Records.get(i)[j].toString());
                        Records.set(j, "CouldNotBeParsed");
                    }else
                    {
                        Logging.info(Records.get(i)[j].toString()+ " has been sanitized");
                    }
                }
            }
        }

What is the proper way to replace this record using the Records.set() ?

3 Answers 3

2

You have an ArrayList of String[], and you are trying to give it a String. You need to be setting the index of the inner String[], not the outer ArrayList.

Do this instead:

Records.get(i)[j] = "CouldNotBeParsed";
Sign up to request clarification or add additional context in comments.

Comments

0

Since you are not replacing the entire array, but changing a single entry in an existing array, you need to use a get() followed by an array write, instead of a set():

if(!validateRecords(Records.get(i)[j].toString())) {
    Logging.info("Records could not be parsed " + Records.get(i)[j]);
    Records.get(i)[j] = "CouldNotBeParsed";
} else {
    Logging.info(Records.get(i)[j] + " has been sanitized");
}

Note that your if (j == 0) check inside the loop looks highly suspicious, because in effect it makes the loop on j entirely useless. You might as well write this:

for (int i = 0; i < Records.size(); i++) {
    if(!validateRecords(Records.get(i)[0].toString())) {
        Logging.info("Records could not be parsed " + Records.get(i)[0]);
        Records.get(i)[0] = "CouldNotBeParsed";
    } else {
        Logging.info(Records.get(i)[0] + " has been sanitized");
    }
}

Also note that calls of toString() are not necessary when you concatenate strings: Java compiler will insert them for you, and also take care of null values for you.

2 Comments

Last question for you. I was thinking. Perhaps I should just add another record to the record instead of replacing it. how would that look like. Records.add((i)[j] = "notParsed")
@user1158745 That's the problem with using arrays - you cannot add items to them. If you want something to which you could add records, use ArrayList<ArrayList<String>>. Then you would write Records.get(i).add("notParsed").
0

If you have an ArrayList<String[]> then it will hold only arrays of String. However you have the following line:

Records.set(j, "CouldNotBeParsed");

"CouldNotBeParsed" is a String, not a String[]. If you really want a String[] try this:

Records.set(j, new String[]{"CouldNotBeParsed"});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.