我正在尝试使用ArrayList编写一个简单的程序,它添加值,删除值和打印值。但是,我在删除用户输入的值时遇到问题。用户应输入指定数量的值(由用户确定),然后程序应删除这些值(如果有重复,那么它应该只删除数字的第一个实例,这是正常的)。它不会删除值,在某些情况下会删除一个值。我对我的代码被窃听的地方感到有点困惑。这是我删除值的代码:
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");
}
这是我的完整代码:
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):");
}
}
答案 0 :(得分:1)
<body ng-controller="MainCtrl">
<div id="groupOptions">
<label><input type="checkbox" name="test1" ng-model="testModel.item1" ng-checked="testModel.item1" /> Testing</label>
<label><input type="checkbox" name="test2" ng-model="testModel.item2" ng-checked="testModel.item2" /> Testing 2</label>
<label><input type="checkbox" name="test3" ng-model="testModel.item3" ng-checked="testModel.item3" /> Testing 3</label>
</div>
<pre>{{testModel|json}}</pre>
<script>
$("#groupOptions").controlgroup();
</script>
删除此列表中指定位置的元素。将任何后续元素向左移位(从索引中减去一个)。
这是您的代码的错误
有关详细信息,请参阅以下链接
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove(int),