在java中打印数组的播放器对象

时间:2014-10-21 12:09:17

标签: java arrays object printing

我有一个带有两个方法的数组类,这两个方法都是必需的。 我有一个算法" maxAtEnd"比较一组数字并将最高数字放在集合的末尾,但将其他数字留在原始位置。 第一种方法为整数数组实现此算法。这很好用。并在之前(第一次循环)和之后(第二次循环)打印数字的顺序。

Thei第二种方法是通用的,应该看起来像代码中所示。应该使用一系列玩家(名称,得分)来测试该方法,即它应该比较玩家阵列的元素(每个元素由玩家的名字和得分组成)。因此我有一个Class" Players"它位于同一个包中,因此我可以创建Player实例并将它们放在一个数组中。

我有一个测试类,我在其中创建数组和播放器实例。第一种方法适用于整数数组。但是第二种方法只打印数组的引用,而不是以适当的顺序(之前/之后)打印元素(播放器),最后使用最高的播放器。我总是得到参考资料。

public class MArrays {


//First method
static public void maxAtEnd(int [] array){  
    int max = array[0];
    int index = 0;
    for (int i=1; i< array.length; i++){
        System.out.print(array[i]+", ");
        if (array[i] > max) {
            index = i;
            max = array[i];
        }
    }
    System.out.println("Before: Max = "+max+" at position "+ (index)+" ---First method");


    int l = array[array.length-1];
    int m = array[index];
    array[array.length-1] = m;
    array[index] = l;

    for (int i=0; i< array.length; i++){
        System.out.print(array[i]+", ");
    }
    System.out.println("After: Max = "+max+" at position "+ (array.length)+" ---second method");


}


// second method
static public <E extends Comparable <? super E>> void maxAtEnd(E[] array){

    E max = array[0];
    int index = 0;
    for (int i=1; i< array.length; i++){
        System.out.print(array[i]+", ");
        if ( array[i].compareTo(array[index]) > 0) {
            index = i;
            max = array[i];
    }
}
    System.out.println("SecondMethod-Before: Max = "+max+" at position "+ (index+1)+" +++second method");

    E l = array[array.length-1];
    E m = array[index];
    array[array.length-1] = m;
    array[index] = l;


    for (int i=0; i< array.length; i++){
        System.out.print(array[i]+", ");            
    }
    System.out.println("SecondMethod-After: Max = "+max+" at position "+ (array.length)+" +++second method");
    }
public static void maxAtEnd(Spieler E) {
    // TODO Auto-generated method stub

}
}

测试类看起来像这样:

public class MArraysTest {

public static void main(String[] args) {

    int array1[] = {100, 200, 420, 300, 130 };
    String array2[] = {"Peter", "Brad", "Anthony", "Michael", "Claudia"};

    Player player1 = new Player("Peter", 100);
    Player player2 = new Player("Brad", 20);
    Player player3 = new Player("Claudia", 150);

    Player[] PlayerList1 = {player1};
    Player[] PlayerList2 = {player1, player2, player3};



    MArrays.maxAtEnd(array1); //this works fine
    System.out.println();

    MArrays.maxAtEnd(array2); // this works fine too, it accesses the second method
    System.out.println();

    MArrays.maxAtEnd(PlayerList1); // these two only print the references and not the name and score
    MArrays.maxAtEnd(PlayerList2);      
}
}

Player类看起来像是在同一个包中,所以我可以从另一个类访问它来创建Player的实例:

public class Player implements Comparable<Player>{


private String Name;
private int Score;

    public Player(String Name, int Score) {
    this.Name=Name;
    this.Score=Score;
    }

public String getName(){
    return Name;
}
public int getScore(){
    return Score;
}

那么当使用MArraysTest中的Player对象数组调用MArrays中的泛型方法时,如何实现print方法来打印Name和Score? 我用toString()等尝试了几件事,但没有任何效果。我总是得到对象的引用。 我安装了Java 8。

1 个答案:

答案 0 :(得分:-1)

您需要先override课程中toString() Player

然后使用

Arrays.toString(arr)

例如:

覆盖toString()

@Override
public String toString() {
    return "Player{" +
            "Name='" + Name + '\'' +
            ", Score=" + Score +
            '}';
}

现在

Player player1 = new Player("Peter", 100);
Player player2 = new Player("Brad", 20);
Player player3 = new Player("Claudia", 150);

Player[] PlayerList1 = {player1};
Player[] PlayerList2 = {player1, player2, player3};
System.out.println(Arrays.toString(PlayerList1));
System.out.println(Arrays.toString(PlayerList2));

Out put:

[Player{Name='Peter', Score=100}]
[Player{Name='Peter', Score=100}, Player{Name='Brad', Score=20}, Player{Name='Claudia', Score=150}]