在java中,如何从同一索引的三个并行数组中删除元素?
我需要让用户输入他们想要删除的名称,然后在保存该名称的数组中找到的索引。然后我需要删除名称以及价格和评级。我可能只使用并行阵列。我不确定其他部分是否成功运行,因为我正在尝试使用.remove(),但出现错误:在java中,如何从同一索引的三个并行数组中删除元素?,java,arrays,parallel-arrays,Java,Arrays,Parallel Arrays,我需要让用户输入他们想要删除的名称,然后在保存该名称的数组中找到的索引。然后我需要删除名称以及价格和评级。我可能只使用并行阵列。我不确定其他部分是否成功运行,因为我正在尝试使用.remove(),但出现错误: cannot find symbol symbol: method remove(int) location: variable array1 of type String[] 代码 public static void removeGames(扫描仪键盘,字符串[]数组1, 双[]
cannot find symbol
symbol: method remove(int)
location: variable array1 of type String[]
代码
public static void removeGames(扫描仪键盘,字符串[]数组1,
双[]阵列2,双[]阵列3,int currentLength)
{
字符串删除输入;
System.out.println(“输入要删除的游戏名称”
+“从列表中:”;
removeInput=keyboard.next();
for(int i=0;i对于数组
没有删除
方法。您可以使用该方法。出现此错误的原因是Java中的数组对象没有.remove()
方法。如果您确实想要一个可以从中删除对象的动态集合,那么应该使用ArrayList
只需将方法签名中的数组替换为ArrayList,然后在正文中将array1[i]
替换为array1。获取(i)
如下所示:
public static void removeGames(Scanner keyboard, ArrayList<String> array1,
ArrayList<Double> array2, ArrayList<Double> array3, int currentLength) {
String removeInput;
System.out.println("Enter the name of the game you would like to remove"
+ " from the list: ");
removeInput = keyboard.next();
for(int i = 0; i < array1.length; i++) {
if(removeInput.equalsIgnoreCase(array1.get(i)) {
array1.remove(i);
array2.remove(i);
array3.remove(i);
}
}
}
public static void removeGames(扫描仪键盘,ArrayList array1,
ArrayList array2,ArrayList array3,int currentLength){
字符串删除输入;
System.out.println(“输入要删除的游戏名称”
+“从列表中:”;
removeInput=keyboard.next();
for(int i=0;i
只需确保导入java.util.ArrayList
一些东西
数组没有remove()方法。如果要在数组数据结构上执行该操作,请使用ArrayList
使用并行数组可能会造成混乱。相反,请将所有信息放在它自己的对象中:
class Game {
String name;
double price, rating;
}
然后你可以写:
ArrayList<Game> games = new ArrayList<Game>();
arraylistgames=newarraylist();
如果您真的需要使用数组,您应该编写自己的方法来删除所需的元素。由于java在java.util
包中有大量容器,我建议您从那里使用一个容器。由于您需要访问给定索引中的元素,我建议您使用。如果您知道索引,并且只需要要从中删除元素,请使用
我还建议根据接口进行编码,因此您的代码如下所示:
public static void removeGames(Scanner keyboard, List<String> array1,
List<Double> array2, List<Double> array3) {
String removeInput;
System.out.println("Enter the name of the game you would like to remove"
+ " from the list: ");
removeInput = keyboard.next();
int index = array1.indexOf(removeInput);
array1.remove(index);
array2.remove(index);
array3.remove(index);
}
public static void removeGames(扫描仪键盘、列表阵列1、,
列表阵列2、列表阵列3){
字符串删除输入;
System.out.println(“输入要删除的游戏名称”
+“从列表中:”;
removeInput=keyboard.next();
int index=array1.indexOf(removeInput);
阵列1.移除(索引);
阵列2.移除(索引);
阵列3.移除(索引);
}
你从哪里知道数组类型有一个remove(int)
方法?你一开始不使用并行数组。使用一个三元组数组。更好的设计方法是创建一个游戏类,该类包含成员变量,如name、price和rating;然后有一个列表-你可以调用remove()我的java教科书上说,array.remove(1)删除索引1中存储的内容,并将索引2中的内容移动到索引1。除非array引用的是ArrayList
(嗯,list.remove
,其中ArrayList是最常见的)(好吧,Collection.remove
将是该方法的来源:P)另外,La comadreja对如何使用游戏对象以及如何只使用一个游戏数组列表给出了很好的建议。我建议您也这样做。我没有将这一点纳入我的回答中,因为我只想说数组列表比数组更适合这项工作。
public static void removeGames(Scanner keyboard, List<String> array1,
List<Double> array2, List<Double> array3) {
String removeInput;
System.out.println("Enter the name of the game you would like to remove"
+ " from the list: ");
removeInput = keyboard.next();
int index = array1.indexOf(removeInput);
array1.remove(index);
array2.remove(index);
array3.remove(index);
}