sort方法返回类的名称而不是Object的参数

时间:2017-05-20 08:25:05

标签: sorting arraylist

我想从代码中得到的是将ArrayList中存储的数字显示为字符串,但我会在标准输出中显示:

Sorted: [randomnumber.RandomNumber@548c4f57, randomnumber.RandomNumber@1218025c [...]

代码如下所示:

package randomnumber;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class RandomNumber {

private String number;
public RandomNumber (String number) {
this.number = number;
}
public String getNumber() {
return number;
}


public static void main(String[] args) {

   RandomNumber n1 = new RandomNumber ("1"); 
   RandomNumber n26 = new RandomNumber ("26");
   RandomNumber n2 = new RandomNumber ("2"); 
   RandomNumber n27 = new RandomNumber ("27");
   RandomNumber n3 = new RandomNumber ("3"); 
   RandomNumber n28 = new RandomNumber ("28");
   RandomNumber n4 = new RandomNumber ("4"); 
   RandomNumber n29 = new RandomNumber ("29");
   RandomNumber n5 = new RandomNumber ("5"); 
   RandomNumber n30 = new RandomNumber ("30");
   RandomNumber n6 = new RandomNumber ("6"); 
   RandomNumber n31 = new RandomNumber ("31");
   RandomNumber n7 = new RandomNumber ("7"); 
   RandomNumber n32 = new RandomNumber ("32");
   RandomNumber n8 = new RandomNumber ("8"); 
   RandomNumber n33 = new RandomNumber ("33");
   RandomNumber n9 = new RandomNumber ("9"); 
   RandomNumber n34 = new RandomNumber ("34");
   RandomNumber n10 = new RandomNumber ("10"); 
   RandomNumber n35 = new RandomNumber ("35");
   RandomNumber n11 = new RandomNumber ("11"); 
   RandomNumber n36 = new RandomNumber ("36");
   RandomNumber n12 = new RandomNumber ("12"); 
   RandomNumber n37 = new RandomNumber ("37");
   RandomNumber n13 = new RandomNumber ("13"); 
   RandomNumber n38 = new RandomNumber ("38");
   RandomNumber n14 = new RandomNumber ("14"); 
   RandomNumber n39 = new RandomNumber ("39");
   RandomNumber n15 = new RandomNumber ("15"); 
   RandomNumber n40 = new RandomNumber ("40");
   RandomNumber n16 = new RandomNumber ("16"); 
   RandomNumber n41 = new RandomNumber ("41");
   RandomNumber n17 = new RandomNumber ("17"); 
   RandomNumber n42 = new RandomNumber ("42");
   RandomNumber n18 = new RandomNumber ("18"); 
   RandomNumber n43 = new RandomNumber ("43");
   RandomNumber n19 = new RandomNumber ("19"); 
   RandomNumber n44 = new RandomNumber ("44");
   RandomNumber n20 = new RandomNumber ("20");
   RandomNumber n45 = new RandomNumber ("45");
   RandomNumber n21 = new RandomNumber ("21"); 
   RandomNumber n46 = new RandomNumber ("46");
   RandomNumber n22 = new RandomNumber ("22"); 
   RandomNumber n47 = new RandomNumber ("47");
   RandomNumber n23 = new RandomNumber ("23");
   RandomNumber n48 = new RandomNumber ("48");
   RandomNumber n24 = new RandomNumber ("24"); 
   RandomNumber n49 = new RandomNumber ("49");
   RandomNumber n25 = new RandomNumber ("25"); 
   RandomNumber n50 = new RandomNumber ("50");

   List <RandomNumber> lotteryList = new ArrayList <>();


   lotteryList.add(n1);
   lotteryList.add(n13);
   lotteryList.add(n25);
   lotteryList.add(n37);
   lotteryList.add(n2);
   lotteryList.add(n14);
   lotteryList.add(n26);
   lotteryList.add(n38);
   lotteryList.add(n3);
   lotteryList.add(n15);
   lotteryList.add(n27);
   lotteryList.add(n39);
   lotteryList.add(n4);
   lotteryList.add(n16);
   lotteryList.add(n28);
   lotteryList.add(n40);
   lotteryList.add(n5);
   lotteryList.add(n17);
   lotteryList.add(n29);
   lotteryList.add(n41);
   lotteryList.add(n6);
   lotteryList.add(n18);
   lotteryList.add(n30);
   lotteryList.add(n42);
   lotteryList.add(n7);
   lotteryList.add(n19);
   lotteryList.add(n31);
   lotteryList.add(n43);
   lotteryList.add(n8);
   lotteryList.add(n20);
   lotteryList.add(n32);
   lotteryList.add(n44);
   lotteryList.add(n9);
   lotteryList.add(n21);
   lotteryList.add(n33);
   lotteryList.add(n45);
   lotteryList.add(n10);
   lotteryList.add(n22);
   lotteryList.add(n34);
   lotteryList.add(n46);
   lotteryList.add(n11);
   lotteryList.add(n23);
   lotteryList.add(n35);
   lotteryList.add(n47);
   lotteryList.add(n12);
   lotteryList.add(n24);
   lotteryList.add(n36);
   lotteryList.add(n48);
   lotteryList.add(n49);
   lotteryList.add(n50);


   Collections.sort (lotteryList, (RandomNumber p1, RandomNumber p2) ->
p1.getNumber().compareTo(p2.getNumber()));
System.out.println("Sorted: " + lotteryList);
}

}

我想从代码中得到的是显示:

Sorted : 1, 2, 3, 4, 5 [...] instead of 

Sorted: [randomnumber.RandomNumber@548c4f57, randomnumber.RandomNumber@1218025c [...]

1 个答案:

答案 0 :(得分:0)

您需要覆盖java.lang.Object

中定义的“toString”方法
public class RandomNumber {

    private String number;

    public RandomNumber (String number) {
        this.number = number;
    }
    public String getNumber() {
        return number;
    }

    @Override
    public String toString{
        return number;
    }

}