Good afternoon, I’m currently stuck at figuring out how to remove specific items like "," and "-" from a list.
Here is my code:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Code{
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
String coords = scanner.next();
int a = 0;
int b = 0;
int c = 0;
int d = 0;
String tan[] = coords.split("");
List<String> ban = new ArrayList<String>();
ban = Arrays.asList(tan);
for (Object str : ban) {
if(ban.contains(",") && ban.contains(("-"))) {
ban.remove(",");
ban.remove("-");
}
}
}
}
Do I need to use iterators?
>Solution :
Your question is titled:
How to remove specific items in array list in java?
But, your code is not trying to do that.
Here is the problem:
List<String> ban = new ArrayList<String>();
This is syntax sugar for:
List<String> ban;
ban = new ArrayList<String>();
That second statement makes a new arraylist and then assigns a pointer to it to the ‘ban’ variable (all non-primitives in java are always references. ban is not ‘an arraylist’. It is a variable that is currently referencing the list you made. ArrayList is like a house (and new ArrayList builds a new house), and ban is like a page in an address book. It’s not the house – it’s directions to a house. ban = wipes out the page and scribbles a new address down).
You then immediately do:
ban = Arrays.asList(mom);
Okay, so now arraylist you just made is immediately tossed in the trash. Nothing refers to it anymore, you’ve overwritten your ‘ban’ variable: It new points at whatever Arrays.asList gives you. In other words, you build a new house, write the address to it down on a piece of paper, then immediately wipe this address out, call a contractor to build a completely different house someplace else, and write the address of that house down).
We then get to the problem: The lists created by Arrays.asList aren’t ArrayLists. They are weirdo lists that you really do not want to ever use. They ‘mimic’ arrays, and that’s bad because java arrays are kinda silly. Specifically:
- This list cannot grow or shrink; as a consequence,
.clear(),.add(),.addAll(),.remove(), and all that jazz does not work and throws exceptions. - The list is just a light wrapper around that array. Anything you do to it also changes the underlying array.
- The array isn’t immutable.
.set(idx, newValue)does work.
You then loop through each element but this isn’t at all what you want – you don’t want – you never use str (the variable) in your loop. You are looping over the list and for each time you loop, you ask the list to remove ALL the commas and dashes. In addition that wouldn’t actually work (you can’t mess with a collection in the middle of iterating it).
In other words, your code has many bugs. Fixing them all:
List<String> ban = new ArrayList<String>(Arrays.asList(mom));
This makes an actual ArrayList, initialized with the contents of the arrays. As an actual arraylist it can grow and shrink – it does support remove.
THen, get rid of the for loop. You can just invoke list.remove(",") on this. Your current code only does this if the list contains both at leat one comma and at least one dash, I’m not sure if you intended for that, either. That leaves:
String coords = scanner.next();
List<String> ban = new ArrayList<String>(Arrays.asList(coords.split("")));
ban.remove(",");
ban.remove("-");
Which is also a lot simpler and easier to read, in addition to not throwing exceptions 🙂
It sounds like your actual intent is to turn this stuff into a list of integers. You don’t need removal in the first place then:
var digits = new ArrayList<Integer>();
for (char c : scanner.next().toCharArray()) {
int d = Character.digit(c, 10);
if (d != -1) digits.add(d);
}
The Character.digit method turns a character that represents a digit into the actual number it represents (and 10 is the base. I think you want decimal here, hence, 10). It returns -1 if the character isn’t a digit (e.g. a comma or a dash or a space). Hence, if it isn’t -1, we add it.