删除括号[]和逗号Java

时间:2017-01-21 19:40:16

标签: java list sorting arraylist brackets

我试图按学生姓氏按升序对列表进行排序并显示列表,但我想删除返回null的[,]。有没有办法让我看不到。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StudentTest  {

    public static void main(String args[]) {
        List<Student> list = new ArrayList<Student>();



       list.add(new Student(" Gracia", "50","\tCOP2250, COP3250, COP4250"));
       list.add(new Student(" Jones", "30", "\tCOP1210, COP3337, COP3530"));
       list.add(new Student(" Smith", "10", "\tCOP2250, COP3250, COP4250"));
       list.add(new Student(" Wilson", "20", "\tWNC1105, ENC3250, REL2210"));
       list.add(new Student(" Braga", "10", "\tENC1105, ENC3250, ISO4250"));
       list.add(new Student(" Adams", "20", "\tWNC1105, ENC3250, REL2210"));
       list.add(new Student(" Giron", "60","\tCOP1210, COP3337, COP3530"));
       list.add(new Student(" O'Neal", "45","\tENC1105, ENC3250, REL2210"));
       list.add(new Student(" Ervin", "40",  "\tENC1105, COP3250, ISO4250"));
       list.add(new Student(" Bourne", "70","\tCOP2250, ENC3250, COP3530"));


        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);

    }
}

class Student implements Comparable<Student> {


    public Student(String name, String id, String course) {
        this.name = name;
        this.id = id;
        this.course = course;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCourse() {
        return course;
    }


    public Student(String course) {
        this.course = course;
    }

    private String name;
    private String id;
    private String course;



    @Override
    public int compareTo(Student student) {

        return name.compareTo(student.name);

    }


    @Override
    public String toString() {


       System.out.println("" + id + name + course );

        return "";

    }
}

输出如下:

  

10 Smith COP2250,COP3250,COP4250

     

20 Wilson WNC1105,ENC3250,REL2210

     

10 Braga ENC1105,ENC3250,ISO4250

     

20 Adams WNC1105,ENC3250,REL2210

     

60 Giron COP1210,COP3337,COP3530

     

45 O&#39; Neal ENC1105,ENC3250,REL2210

     

40 Ervin ENC1105,COP3250,ISO4250

     

70 Bourne COP2250,ENC3250,COP3530

     

[,,,,,,,,,,

为什么我得到这条线?

  

[,,,,,,,,,,

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

执行System.out.println(list);时,您只需使用ArrayList.toString()方法的默认实现,该方法返回[]括号中的逗号和空格分隔的列表中的值。 你有两个选择:

  1. 单独浏览列表并单独打印每个学生(只要它具有toString()方法实现。

  2. 或者您可以使用replaceAll()为list.toString()

  3. 提供的字符串

    一般情况下,第一个选项是首选,因为对于常见情况"[" "]" ", "可以是列表元素中的有效字符,不得替换。 但是,对于小型案例,如果您确定Student name, id or course中没有此类字符,则可以执行此操作。

答案 1 :(得分:0)

当您使用System.out.println(list)时,您正在使用List的默认实现来向控制台打印内容。

默认实现遍历列表并调用每个对象的toString。在Student类中覆盖该函数是个好主意。但你刚刚回来了#34;&#34;。默认实现创建一个新String。它以&#34; [&#34;开头。接下来是列表中每个对象的toString方法的返回值(由&#34;,&#34;分隔)。它以&#34;]&#34;结尾。你的toString方法返回一个空字符串&#34;&#34;。这意味着默认实现将创建以下内容:

  

[,,,,,,,,,,

您现在可以更改toString方法的返回值:

public String toString() {
   return "" + id + name + course ;
}

您将获得以下输出:

  

[50 Gracia COP2250,COP3250,COP4250,30 Jones COP1210,COP3337,COP3530,10 Smith COP2250,COP3250,COP4250,20 Wilson WNC1105,ENC3250,REL2210,10 Braga ENC1105,ENC3250,ISO4250,20 Adams WNC1105,ENC3250, REL2210,60 Giron COP1210,COP3337,COP3530,45 O&#39; Neal ENC1105,ENC3250,REL2210,40 Ervin ENC1105,COP3250,ISO4250,70 Bourne COP2250,ENC3250,COP3530]

如果您不想使用逗号和括号,您可以编写自己的列表,从ArrayList扩展,或者遍历列表,而不是打印它。

   for(Student s : list){
       System.out.println(s);
   }

然后您将获得以下输出:

  

50 Gracia COP2250,COP3250,COP4250

     

30 Jones COP1210,COP3337,COP3530

     

10 Smith COP2250,COP3250,COP4250

     

20 Wilson WNC1105,ENC3250,REL2210

     

10 Braga ENC1105,ENC3250,ISO4250

     

20 Adams WNC1105,ENC3250,REL2210

     

60 Giron COP1210,COP3337,COP3530

     

45 O&#39; Neal ENC1105,ENC3250,REL2210

     

40 Ervin ENC1105,COP3250,ISO4250

     

70 Bourne COP2250,ENC3250,COP3530

答案 2 :(得分:0)

如果您不希望在列出 list ArrayList数组时看到方括号,那么请不要只使用:

System.out.println(list);

您应该遍历数组,以便按照您希望的方式处理每个数组元素,例如:

for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i));
}

现在没有方括号。

您的 main()方法代码应如下所示:

public static void main(String args[]) {
    List<Student> list = new ArrayList<>();

    list.add(new Student(" Gracia", "50", "\tCOP2250, COP3250, COP4250"));
    list.add(new Student(" Jones", "30", "\tCOP1210, COP3337, COP3530"));
    list.add(new Student(" Smith", "10", "\tCOP2250, COP3250, COP4250"));
    list.add(new Student(" Wilson", "20", "\tWNC1105, ENC3250, REL2210"));
    list.add(new Student(" Braga", "10", "\tENC1105, ENC3250, ISO4250"));
    list.add(new Student(" Adams", "20", "\tWNC1105, ENC3250, REL2210"));
    list.add(new Student(" Giron", "60", "\tCOP1210, COP3337, COP3530"));
    list.add(new Student(" O'Neal", "45", "\tENC1105, ENC3250, REL2210"));
    list.add(new Student(" Ervin", "40", "\tENC1105, COP3250, ISO4250"));
    list.add(new Student(" Bourne", "70", "\tCOP2250, ENC3250, COP3530"));

    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }

    Collections.sort(list);

    System.out.println(""); 
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
}

现在,正如@Leon在评论中已经说明的那样,您已经在学生类中错误地设置了 toString()方法。您应该将数据作为字符串返回而不是显示它。您的 toString()方法应如下所示:

@Override
public String toString() {
    return id + name + course;
}

这应该照顾好事情。您的输出应如下所示:

50 Gracia   COP2250, COP3250, COP4250
30 Jones    COP1210, COP3337, COP3530
10 Smith    COP2250, COP3250, COP4250
20 Wilson   WNC1105, ENC3250, REL2210
10 Braga    ENC1105, ENC3250, ISO4250
20 Adams    WNC1105, ENC3250, REL2210
60 Giron    COP1210, COP3337, COP3530
45 O'Neal   ENC1105, ENC3250, REL2210
40 Ervin    ENC1105, COP3250, ISO4250
70 Bourne   COP2250, ENC3250, COP3530

20 Adams    WNC1105, ENC3250, REL2210
70 Bourne   COP2250, ENC3250, COP3530
10 Braga    ENC1105, ENC3250, ISO4250
40 Ervin    ENC1105, COP3250, ISO4250
60 Giron    COP1210, COP3337, COP3530
50 Gracia   COP2250, COP3250, COP4250
30 Jones    COP1210, COP3337, COP3530
45 O'Neal   ENC1105, ENC3250, REL2210
10 Smith    COP2250, COP3250, COP4250
20 Wilson   WNC1105, ENC3250, REL2210