开发者

Java - remove strings from an ArrayList that contains the specified

开发者 https://www.devze.com 2023-03-13 04:01 出处:网络
Sometimes, due to faulty data collected, a line generated by the following method ends up looking like this when saved: \",-1,0\" or something similar, with no name, an ID of -1 and a level of 115 or

Sometimes, due to faulty data collected, a line generated by the following method ends up looking like this when saved: ",-1,0" or something similar, with no name, an ID of -1 and a level of 115 or something else. (The lines are formatted like this (excluding quotes): "name,id,level" (e.g: "Honour guard,5514,115")

What i need to do is to remove all strings in monstersToAdd that contains -1. I've tried this, but with no success:

private void combineInfo() {
    for(int i = 0;开发者_如何学Go i < monsterList.size(); i++){
        monstersToAdd.add("" + names[i] + "," + IDs[i] + "," + levels[i]);
    }
    monstersToAdd.remove(monstersToAdd.contains("-1"));
}

with the line monstersToAdd.remove(monstersToAdd.contains("-1")); I was trying to remove all strings in monstersToAdd that contains "-1". This however does not work, probably for good reasons, which I unfortunately don't know of yet.

I would really appreciate any input :).


You would be better off not adding the lines you don't want in the first place.

for (....) {
  if (IDs[i] != -1) {
    // add it
  }
  // else it simply doesn't get added
}

More on your original code: You could post a little more detail, such as the type of monsterToAdd. If it is a non-generic list, then the contains method just returns true or false depending if the parameter (here a string of "-1") is present in the list exactly as you pass it, that is it doesn't search for substring matches of the list elements.

remove then tries to remove the element you ask to remove, which may be a Boolean object, automatically boxed from the boolean primitive value returned by contains.

Also, it is suspicious that you have a variable called monsterList which you use for iteration length, but not actually use any elements from that list. Maybe the arrays you use have the same values as the list, and were copied out beforehand? If so, it would be nicer to iterate on the monsterList directly and use its elements.


Its easier if you dont even add them, than adding and removing them so check the sanity of ID names and levels before adding them

private void combineInfo() {
for(int i = 0; i < monsterList.size(); i++){
    //add only if name is non empty, ID is not negative and level is below 100
    if(!(names[i].isEmpty() || IDs[i]<0 || levels[i]>100))
    monstersToAdd.add("" + names[i] + "," + IDs[i] + "," + levels[i]);
}


Why don't you do this instead:

private void combineInfo() {
for(int i = 0; i < monsterList.size(); i++){
    if(IDs[i] != -1){
        monstersToAdd.add("" + names[i] + "," + IDs[i] + "," + levels[i]);
    }
}
monstersToAdd.remove(monstersToAdd.contains("-1"));

}

That way, you never add the monster to the list in the first place, if the ID is -1.


You are really close:

private void combineInfo() {
    for(int i = 0; i < monsterList.size(); i++){
        if (IDs[i] == -1) continue; // Skip this iteration
        monstersToAdd.add("" + names[i] + "," + IDs[i] + "," + levels[i]);
    }
}

Filter them out as early as possible rather than back-tracking and removing them.


contains() returns only a true/false result depending on whether the list contains the given input object (in your case the string "-1"). So in your example, your list wouldn't contain "-1", so your remove statement would be resolved to this:

monstersToAdd.remove(false);

which wouldn't work for obvious reasons.


Here is the code:

for(Iterator<String> it = monsterList; it.hasNext();) {
    String elem = it.next();
    if (elem.contains("-1")) {
         it.remove();
    }
}

contains() method of collection returns true if collection contains element equals to one passed as an argument. In you case you want to use String's contains() that returns true if the string contains specified substring. This is the reason that you need loop. This loop must be implemented with iterator. Using new java 5 syntax for(String elem : list) will not work here because you have to remove element. Using for(int i = 0; i < list.size(); i++) requires implementation of logic that safely moves to the next index after element removal.

And the last point. You have to use iterator.remove() instead of Collection.remove() to avoid ConcurrentModificationException

0

精彩评论

暂无评论...
验证码 换一张
取 消