配列に格納されている最大と最小の整数値を出力しようとしています。私は最高を印刷することができますが、最低のために私は正しい結果を得ていません。配列に格納されている最高と最低の番号を見つける方法は?
以下の私の二つのクラス考えてみましょう:正しいか、他のいくつかの間違いがあるない場合
class ArrayUtilityNew
{
public static int findMaxMin(int a[])
{
int n=a.length;
int n2=n-1;
int minNo=0;
int count=0;
int maxNo=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i]<a[j])count++;
}
//This gives the highest no. if the count is 0
if(count==0)
{
maxNo=a[i];
}
//Lowest no. shall be gained here if the count is greater
// than the no of elements in the array
// if(count>n)
// {
// minNo=a[i];
// }
count=0;
}
return maxNo;
}
}
class ArrayInteractionsNew
{
public static void main(String arr[])
{
int one[]={3,20,1999,2,10,8,999};
int answer=ArrayUtilityNew.findMaxMin(one);
System.out.println("The highest no in the array is "+answer);
}
}
は第二の背後にあるロジックですか?
どのように修正できますか?
リストを使用すると効率が向上するのはなぜですか?配列のアクセスは非常に高速であり、動的長さは必要ありません。 – hexafraction
コードが不完全です。値を返すのはどうですか? intを返す関数でminとmaxの値を返すのはどうでしょうか? – ElChupacabra
@ hexafractionこれは1ライナーなので。しかし、ええ、配列は、いくつかの行が必要ですが、より高速です。 – Fouss