我编写了以下方法来查找数组中最小数字的位置/索引。
private static int indexOfMin( int[] a, int cnt )
{
int loc = 0;//the variable that will hold the index position
int min = a[loc];//the variable that will compare the value of loc against its location
for (int i = 1; i < a.length; i++)
{
if (a[i] < min )//if value is less
{
min = a[i];// make value of i equal to min
loc = i; loc takes on value of the index of the value of min
}
}
return loc ;
}
它不返回最小int的位置,而是返回最后一个int的位置。如何找到最小int的位置并将其返回int loc
?
进一步编辑: 这是整个程序。另外两种方法我还在努力。所以忽略它们。
public static void main (String[] args) throws Exception
{
final int CAP = 20; // capacity
Scanner infile = new Scanner( new File(args[0]) );
int[] arr = new int[ CAP ];
int count = 0;
while ( count < arr.length && infile.hasNextInt() )
{
arr[ count ] = infile.nextInt();
count++;
}
infile.close();
printArray( arr, count );
// this method is given to you as as. don't modity it
int minVal = minOf( arr, count );
System.out.println( "Smallest number is: " + minVal);
// YOU MUST WRITE THE DEFINITION (code) BELOW MAIN FOR indexOfMin METHOD
int indOfMin = indexOfMin( arr, count );
System.out.println( "Smallest number is located at index position: " + indOfMin );
// YOU MUST WRITE THE DEFINITION (code) BELOW MAIN FOR maxOf METHOD
int maxVal = maxOf( arr, count );
System.out.println( "Largest number is: " + maxVal);
// YOU MUST WRITE THE DEFINITION (code) BELOW MAIN FOR indexOfMax METHOD
int indOfMax = indexOfMax( arr, count );
System.out.println( "Largest number is located at index position: " + indOfMax );
} // END main
// GIVEN AS IS - DO NOT MODIFY
private static int minOf( int[] a, int cnt )
{
int min = a[0];
for ( int i=0 ; i<cnt ; i++ )
{
if (a[i] < min)
min = a[i];
}
return min;
}
// YOU WRITE DEFINTION OF indexOfMin
// returns the INDEX of the min value NOT the min value itself
private static int indexOfMin( int[] a, int cnt )
{
int loc = 0;
int min = a[loc];
for (int i = 1; i < a.length; i++)
{
if (a[i] < min )
{
min = a[i];
loc = i;
}
}
return loc ;
}
日期文件包含以下信息:
86 95 84 94 32 8 56 51 98 20 90 1 75 6 21
答案 0 :(得分:2)
根据您的评论和编辑,我认为您想要
private static int indexOfMin(int[] a, int cnt) {
int loc = 0;
int min = a[loc];
for (int i = 1; i < cnt; i++) {
if (a[i] < min) {
min = a[i];
loc = i;
}
}
return loc;
}
然后验证,
// this method is given to you as as. don't modity it
int minVal = minOf( arr, count );
System.out.println( "Smallest number is: " + minVal);
// YOU MUST WRITE THE DEFINITION (code) BELOW MAIN FOR indexOfMin METHOD
int indOfMin = indexOfMin( arr, count );
System.out.println( "Smallest number is located at index position: " + indOfMin);
if (arr[indOfMin] == minVal) {
System.out.println("Min value passed");
} else {
System.out.println("Min value failed");
}
答案 1 :(得分:1)
int[] arr = {1, 5, 3, 4, 0, 9};
System.out.println(indexOfMin(arr));
打印:
<强> 4 强>
这是绝对正确的,因为数组的索引以0
开头答案 2 :(得分:0)
发布的第一个方法有效。
你没有问题。
您可能忘记索引从0开始,因此将结果解释为错误?
答案 3 :(得分:0)
如上所述,您的第一段代码工作正常。
java中的数组从index = 0开始。
从它的外观来看,cnt应该是你传递的数据数组的长度,它可以用作a.length的替代(尽管传递额外的数据是非常不必要的)。
在第二段代码中,在完成整个循环之前返回一个索引(可能)。一旦你到达第一个小于[0]的项目并返回该位置(在你的情况下可能出现在索引3),这将停止。
为了将来参考,了解您正在测试的数据将对我们有所帮助。
答案 4 :(得分:0)
第一个代码是正确的。你仍然可以删除从未使用过的cnt。一个是完全错误的。
import java.util.Scanner;
public class small {
private static int indexOfMin( int[] a ,int cnt)
{
int loc=0;
int min = a[0];
for (int i = 1; i <cnt; i++)
{
if (a[i] < min )
{
min = a[i];
loc = i;
}
}
return loc ;
}
public static void main(String args[]){
Scanner s= new Scanner(System.in);
System.out.println("how many numbers do you want to enter:");
int n=s.nextInt();
int a[]=new int[n];
System.out.println("enter the numbers:");
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
int p=indexOfMin(a,n);
System.out.println((p+1)); //p+1 as we have taken location same as i which starts with 0
}
}
您想输入多少个数字:7
输入数字: 34 45 33 234 123 116 555
最小的no的位置是:3
我希望你觉得它有用。
答案 5 :(得分:-1)
为什么不使用Arrays.sort()
并返回数组的第一个元素?
//Snippet
void minInArray(){
int[] arr = new int[]{55,42,11,20,584,63,21,27,84,96,32,30};
int[] cArr = Arrays.copyOf(arr, arr.length);
Arrays.sort(cArr);
int idx = Arrays.asList(arr).indexOf(cArr[0]);
String s = new StringBuilder("The min value is: ")
.append(cArr[0])
.append(" located at index: ")
.append(idx)
.toString();
System.out.println(s);
}