代码在数组中的最后一个值不正确

时间:2015-12-02 20:04:47

标签: java arrays arraylist

我无法找到为什么代码具有测试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

1 个答案:

答案 0 :(得分: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但我不确定)。