2015-12-02 104 views
-3

不正確最後的值,我找不到爲什麼代碼對測試3和5。我不能改變數組的數組列表的最後一個不正確的值。方法是我可以改變的唯一的東西。代碼已經在陣列

package Semester; 
import java.util.ArrayList; 


public class MemberArrayList { 
    private ArrayList<Integer> friendsScore = new ArrayList<Integer>(); 
    private ArrayList<String> friendsID = new ArrayList<String>(); 
    // Add a friend with ID with the score into friends. 
    public void addFriend(String friendID, int score) { 
     // TODO 
     friendsScore.add(score); 
     friendsID.add(friendID); 
    } 
    // Removes a friend with friendID, but you cannot use indexOf. Only methods you can use: size, equals, get, remove 
    public void removeFriend(String friendID) { 
     // TODO 
     for (int i = 0; i < friendsID.size(); i++) 
     { 
      if (friendID.equals(friendsID.get(i))) 
      { 
       friendsScore.remove(i); 
       friendsID.remove(i); 
      } 
     } 
    } 
    // Get the scores of first 10 friends added 
    public int [] getScores10() { 
     // TODO 
     int size = 10; 
     int[] score = new int[size]; 
     for (int i = 0; i < 10; i++) 
     { 
      score[i] = friendsScore.get(i); 
     } 

     return score; 
    } 
    public int [] getScoresLast10() { 
     // TODO 
     int[] score = new int[10]; 
     int j = 0; 
     for (int i = friendsScore.size()-1; i > (friendsScore.size()-10); i--) 
     { 
      score[j] = friendsScore.get(i); 
      j++; 
     } 

     return score; 
    } 
    // Get the scores of the friends in the array, but you cannot use indexOf function. You can only use size, get, equals, intValue 
    public int [] getScores(String [] friendIDs) { 
     // TODO 
     int value = friendIDs.length; 
     int[] score = new int[value]; 
     for (int i = 0; i < value; i++) 
     { 
      String person = friendIDs[i]; 
      for (int j = 0; j < friendsID.size(); j++) 
      { 
       if (person.equals(friendsID.get(j))) 
       { 
        score[i] = friendsScore.get(j); 
       } 
      } 
     } 
     return score; 
    } 

    public static void main(String[] args) { 
     MemberArrayList member = new MemberArrayList(); 
     member.addFriend("Paul", 3); 
     member.addFriend("Peter", 1); 
     member.addFriend("Mary", 2); 
     member.addFriend("John", 4); 
     member.addFriend("Karen", 7); 
     member.addFriend("Kevin", 3); 
     member.addFriend("Walter", 1); 

     member.removeFriend("Mary"); 
     member.removeFriend("Walter"); 

     member.addFriend("Steven", 21); 
     member.addFriend("Kelly", 9); 
     member.addFriend("Kaitlin", -5); 
     member.addFriend("Bea", 77); 
     member.addFriend("Max", 32); 

     System.out.println("Test 1"); 
     String [] friendIDs = {"Paul","Kevin","Steven","Max"}; 
     int [] scores = member.getScores(friendIDs); 
     for (int i = 0; i < scores.length; i++) 
      System.out.println(friendIDs[i]+" "+scores[i]); 

     System.out.println("Test 2"); 
     scores = member.getScores10(); 
     for (int i = 0; i < scores.length; i++) 
      System.out.println(scores[i]); 

     System.out.println("Test 3"); 
     scores = member.getScoresLast10(); 
     for (int i = 0; i < scores.length; i++) 
      System.out.println(scores[i]); 

     member.removeFriend("Bea"); 

     member.addFriend("Eric", -1); 
     member.addFriend("Abby", -2); 
     member.addFriend("Jacob", 3); 
     member.addFriend("Blake", 8); 

     System.out.println("Test 4"); 
     scores = member.getScores10(); 
     for (int i = 0; i < scores.length; i++) 
      System.out.println(scores[i]); 

     System.out.println("Test 5"); 
     scores = member.getScoresLast10(); 
     for (int i = 0; i < scores.length; i++) 
      System.out.println(scores[i]); 
    } 

} 

輸出應該

Test 1 
Paul 3 
Kevin 3 
Steven 21 
Max 32 
Test 2 
3 
1 
4 
7 
3 
21 
9 
-5 
77 
32 
Test 3 
32 
77 
-5 
9 
21 
3 
7 
4 
1 
3 
Test 4 
3 
1 
4 
7 
3 
21 
9 
-5 
32 
-1 
Test 5 
8 
3 
-2 
-1 
32 
-5 
9 
21 
3 
7 

回答

0

您的倒計時循環中缺少一個值複製:
錯誤:

for (int i = friendsScore.size()-1; i > (friendsScore.size()-10); i--) 

這將運行9次。
右:

for (int i = friendsScore.size()-1; i >= (friendsScore.size()-10); i--) 

這將運行10次,並將所有10個值的數組。需要注意的>=代替>
隨着第一循環的數組的最後一個值將不會被設置等默認爲0或任何你的編譯器可能會設置(我想它總是在java中0,但我不知道)。

+0

謝謝,我不知道我怎麼錯過了。我多次更改了代碼並找不到它。現在就像魅力一樣 –