Java-通过ArrayList循环扫描匹配值
我正在尝试使用ArrayList编写一个简单的程序,它可以添加值、删除值和打印值。但是,我在删除用户输入的值时遇到了一个问题。用户应输入指定数量的值(由用户确定),然后程序应删除这些值(如果存在重复值,则只需删除数字的第一个实例即可)。它不会删除值,在某些情况下会删除一个值。我有点困惑,我的代码哪里被窃听了。以下是我删除值的代码:Java-通过ArrayList循环扫描匹配值,java,arraylist,Java,Arraylist,我正在尝试使用ArrayList编写一个简单的程序,它可以添加值、删除值和打印值。但是,我在删除用户输入的值时遇到了一个问题。用户应输入指定数量的值(由用户确定),然后程序应删除这些值(如果存在重复值,则只需删除数字的第一个实例即可)。它不会删除值,在某些情况下会删除一个值。我有点困惑,我的代码哪里被窃听了。以下是我删除值的代码: private static void removeVals() { System.out.println("How many values woul
private static void removeVals() {
System.out.println("How many values would you like to remove?");
int amountToRemove = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter " + amountToRemove + " values:");
for(int i = 0; i < amountToRemove; i++) {
int vals = scanner.nextInt();
if(arrayList.get(i).equals(vals)) {
arrayList.remove(i);
}
}
System.out.println("Values removed");
}
private static void removals(){
System.out.println(“要删除多少个值?”);
int amountToRemove=scanner.nextInt();
scanner.nextLine();
System.out.println(“输入”+amountToRemove+”值:”;
对于(int i=0;i
这是我的全部代码:
public class Main {
private static ArrayList<Integer> arrayList = new ArrayList<Integer>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
printOptions();
int option = scanner.nextInt();
while (option != 0) {
switch(option) {
case 1:
addVals();
printOptions();
option = scanner.nextInt();
break;
case 2:
removeVals();
printOptions();
option = scanner.nextInt();
break;
case 3:
printVals();
printOptions();
option = scanner.nextInt();
break;
case 4:
printOptions();
printOptions();
option = scanner.nextInt();
break;
default:
System.out.println("Not a valid option, please enter again:");
option = scanner.nextInt();
break;
}
}
}
private static void addVals() {
System.out.println("How many values would you like to add?");
int amountToAdd = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter " + amountToAdd + " values:");
for(int i = 0; i < amountToAdd; i++) {
int vals = scanner.nextInt();
arrayList.add(vals);
}
}
private static void removeVals() {
System.out.println("How many values would you like to remove?");
int amountToRemove = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter " + amountToRemove + " values:");
for(int i = 0; i < amountToRemove; i++) {
int vals = scanner.nextInt();
if(arrayList.get(i).equals(vals)) {
arrayList.remove(i);
}
}
System.out.println("Values removed");
}
private static void printVals() {
for(int i = 0; i < arrayList.size(); i++) {
System.out.print(arrayList.get(i) + " ");
}
System.out.println();
}
private static void printOptions() {
System.out.println();
System.out.println("Program options: ");
System.out.println("1. Add values\n2. Remove values\n3. Print values\n4. Print options\n0. Exit\n");
System.out.println("\nWhat would you like to do? (enter an option):");
}
}
公共类主{
私有静态ArrayList ArrayList=新ArrayList();
专用静态扫描仪=新扫描仪(System.in);
公共静态void main(字符串[]args){
打印选项();
int option=scanner.nextInt();
while(选项!=0){
开关(选件){
案例1:
addVals();
打印选项();
选项=scanner.nextInt();
打破
案例2:
删除();
打印选项();
选项=scanner.nextInt();
打破
案例3:
printVals();
打印选项();
选项=scanner.nextInt();
打破
案例4:
打印选项();
打印选项();
选项=scanner.nextInt();
打破
违约:
System.out.println(“不是有效选项,请重新输入:”);
选项=scanner.nextInt();
打破
}
}
}
私有静态void addVals(){
System.out.println(“要添加多少值?”);
int amountToAdd=scanner.nextInt();
scanner.nextLine();
System.out.println(“输入”+amountToAdd+”值:”;
对于(int i=0;i
arrayList.remove(i)
删除此列表中指定位置的元素。将任何后续元素向左移动(从其索引中减去一个)。
这就是您的代码中的错误
有关更多详细信息,请参阅下面的链接
,如果要在迭代时删除元素,则应使用迭代器,不要对每个元素使用。@TimBiegeleisen它不是对每个元素使用的。