Java-搜索数组中等于给定数字的所有元素索引
我有searchSales()方法,它应该查找与给定销售数字相等的所有销售数字。应用程序要求用户使用键盘输入给定的销售数字并搜索它。如果找到从键盘输入的销售数字,则应用程序将显示销售数字,否则将显示相应的消息。我有一个代码,它只显示相等销售额的第一个索引,例如:数组有元素1,2,3,3,4,5,我想找到[array]=3的所有索引。我该怎么做Java-搜索数组中等于给定数字的所有元素索引,java,arrays,Java,Arrays,我有searchSales()方法,它应该查找与给定销售数字相等的所有销售数字。应用程序要求用户使用键盘输入给定的销售数字并搜索它。如果找到从键盘输入的销售数字,则应用程序将显示销售数字,否则将显示相应的消息。我有一个代码,它只显示相等销售额的第一个索引,例如:数组有元素1,2,3,3,4,5,我想找到[array]=3的所有索引。我该怎么做 public static void searchSales(int search[]){ Scanner input = new Sc
public static void searchSales(int search[]){
Scanner input = new Scanner(System.in);
System.out.print("Enter sales figure you want to find: ");
int target = input.nextInt();
int index = -1;
for (int i=0; i<search.length; i++){
if (search[i] == target){
index=i;
break;
}
}
if (index == -1){
System.out.println("Sales figure not found");
}
else {
System.out.printf("Sales figure found at branch %d",index+1);
}
}
publicstaticvoidsearchsales(int-search[]){
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入要查找的销售数字:”);
int target=input.nextInt();
int指数=-1;
对于(int i=0;i删除表示break;
(这一行使代码退出循环)的行,将每个值存储在数组中并在末尾打印出来,或者打印出来,而不是将它们存储在index
变量中。使用列表来收集匹配的索引。类似于:
int target = input.nextInt();
List<Integer> indices = new ArrayList<Integer>();
for (int i=0; i<search.length; i++){
if (search[i] == target){
indices.add(i);
}
}
if (indices.isEmpty()){
System.out.println("Sales figure not found");
}
else {
System.out.println("Sales figures found: " + indices);
}
ArrayList<Integer> sales = Arrays.asList(search)
int start = sales.indexOf(target);
int end = sales.lastIndexOf(target);
if(start==end){
System.out.printf("Sales figure found at branch %d",start+1);
}
else{
for(int i=start;i<=end-start;i++)
if(sales.get(i)==target)
System.out.printf("Sales figure found at branch %d",i+1);
}
int target=input.nextInt();
列表索引=新的ArrayList();
对于(int i=0;ipublicstaticvoidsearchsales(int search[])){
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入要查找的销售数字:”);
int target=input.nextInt();
int指数=-1;
对于(int i=0;i使用Arrays.asList,然后在列表上使用indexOf/lastIndexOf。类似于:
int target = input.nextInt();
List<Integer> indices = new ArrayList<Integer>();
for (int i=0; i<search.length; i++){
if (search[i] == target){
indices.add(i);
}
}
if (indices.isEmpty()){
System.out.println("Sales figure not found");
}
else {
System.out.println("Sales figures found: " + indices);
}
ArrayList<Integer> sales = Arrays.asList(search)
int start = sales.indexOf(target);
int end = sales.lastIndexOf(target);
if(start==end){
System.out.printf("Sales figure found at branch %d",start+1);
}
else{
for(int i=start;i<=end-start;i++)
if(sales.get(i)==target)
System.out.printf("Sales figure found at branch %d",i+1);
}
ArrayList sales=Arrays.asList(搜索)
int start=sales.indexOf(目标);
int end=sales.lastIndexOf(目标);
如果(开始==结束){
System.out.printf(“在分支机构%d处找到的销售数字”,起始+1);
}
否则{
对于(int i=start;i,在您的情况下,您实际上不需要在任何地方存储任何东西,类似这样的东西就足够了:
public static void searchSales(int search[]){
Scanner input = new Scanner(System.in);
System.out.print("Enter sales figure you want to find: ");
int target = input.nextInt();
boolean found = false
for (int i = 0 ; i < search.length ; i++)
if (search[i] == target) {
found = true;
System.out.printf("Sales figure found at branch %d", i + 1);
}
if (! found)
System.out.println("Sales figure not found");
}
你的continue
是多余的。你是对的。排序会破坏所需的索引。使其有点混乱。更新。无需继续点击sales.get(start)
,只需与target
:-)进行比较即可使用列表迭代器(start)
但是如果不使用ArrayList,我怎么能做到这一点呢?@Nataly-如果不想使用集合,请检查其他答案。@oldrinb-谢谢您的建议-已更新。不确定listiterator(开始)
在这里添加了任何内容,因为我必须遍历整个列表,而不是停在末尾。
。是的,这更好,而且工作正常!非常感谢!但是如果使用highestSales()方法,我需要做什么?你说的“如果使用highestSales()方法,我需要做什么”是什么意思?如果您是指需要导入的内容:import java.util.ArrayList
和import java.util.List
.List List List=new ArrayList();-它说找不到符号和最后一行中的第二个错误-for(int i:List)System.out.println(i);-不兼容的类型。我如何修复它?对不起,我只是java的初学者。)我修好了。非常感谢。)我正在努力完成我的申请