2

整数の配列リストがあります。最小の 2 つの整数を除いた整数の平均を計算する必要があります。私はこれを行うさまざまな方法を試し続けていますが、私がやりたいことは、min1 を見つけて削除し、次に min2 を見つけて削除することだと思います。

public double computeAverageWithoutLowest2()
  {
    ArrayList<Student> newscores = new ArrayList<Student>(scores);
    int newtotalScores=0;
    int newavg=0; 
    int min1 = 0;
    int min2 = 0;

    min1 = newscores.get(0).getScore();

    for (int i = 0; i< newscores.size(); i++)
    {
      if (newscores.get(i).getScore() < min1)
      {
        min1 = newscores.get(i).getScore();
      }
    }

ここで、配列リストから min1 を削除したいと思います。私は明らかに newscores.remove(min1); を試しました。これは機能しません。配列 min1 内のどのスポットを見つけて削除するにはどうすればよいですか?

どんな助けでも大歓迎です!!

さて、コメントを見て、コードを次のように変更しました。

ArrayList<Student> newscores = new ArrayList<Student>(scores);
    int newtotalScores=0;
    int newavg=0; 

    int minPos1 = 0;
    int minPos2 = 0;
    int min1 = newscores.get(0).getScore();
    int min2 = newscores.get(0).getScore();

    for(int i = 0; i < newscores.size(); i++) 
    {
      if(newscores.get(i).getScore() < min1) 
      {
        min1 = scores.get(i).getScore();
        minPos1 = i;
      } 
    }
    newscores.remove(minPos1);


    for(int j = 0; j < newscores.size(); j++)
    {
      if(newscores.get(j).getScore() < min2)
      {
        min2 = scores.get(j).getScore();
        minPos2 = j;
      }
    }
    newscores.remove(minPos2);

この方法は min1 を削除するのに機能しますが、min2 を削除するのには機能しません。代わりに、min1 が削除したのと同じ位置を削除するだけです。

4

3 に答える 3

5

removeindexOfおよびを単純に使用してみませんかmin

newscores.remove(newscores.indexOf(Collections.min(newscores)));

最小の 2 つのアイテムを削除する場合は、2 回行います。

于 2013-11-09T22:22:12.413 に答える
1

このソリューションは、Collections.min()反復が 1 回で済むため、2 回呼び出すよりも高速ですが、配列を再度調べて最下位の要素を削除することを避けるために、位置も保存する必要があります。

ArrayList<Student> newscores = new ArrayList<Student>(scores);
Student min1;
Student min2;
int minPos1;
int minPos2;

for(int i = 0; i < newscores.size(); i++) {
   if(newscores.get(i).getScore() < min1.getScore()) {
      min1 = student;
      minPos1 = i;
   } else if (newscores.get(i).getScore() < min2.getScore()) {
      min2 = student;
      minPos2 = i;
   }
}
newscores.remove(minPos1);
newscores.remove(minPos2);

別のアプローチは、 のような順序付きコレクションを使用するPriorityQueueことです。これは、順序付けられた要素を入力すると、最低のものを探す必要がないため、より高速です。

于 2013-11-09T22:29:06.267 に答える
1

リストから要素を削除しないでください。繰り返し処理するときに、これまでに見つかった最低の2つを別々に保ち、さらに低い要素が見つかったときにのみ合計に追加してください。このようにして、1回の反復で平均を計算できます。

アイデアを与えるには:

List<Integer> scores = Arrays.asList(2, 3, 4, 5, 0, 6, 1, 7);

int min1 = scores.get(0);
int min2 = scores.get(1);
int sum = 0;
int swap = 0;

for (int i = 2; i < scores.size(); i++) {
    int score = scores.get(i);
    if (score < min1) {
        swap = min1;
        min1 = score;
        score = swap;
    }
    if (score < min2) {
        swap = min2;
        min2 = score;
        score = swap;
    }
    System.out.println("adding " + score);
    sum += score;
}
System.out.println(String.format("lowest scores: %d and %d",min1, min2));
System.out.println(sum / (scores.size() - 2.0));

版画

adding 4 
adding 5
adding 3
adding 6
adding 2
adding 7
lowest scores: 0 and 1
4.5
于 2013-11-09T22:31:54.500 に答える