2017-01-21 260 views
0

我想作爲排序依據學生的姓氏升序排列的列表,並顯示列表中,但我想刪除[,]返回空值。有沒有辦法讓我看不到?刪除括號[]和逗號的Java

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史密斯COP2250,COP3250,COP4250

20威爾遜WNC1105,ENC3250,REL2210

10布拉加ENC1105,ENC3250,ISO4250

20亞當斯WNC1105,ENC3250,REL2210

60 Giron的COP1210,COP3337,COP3530

45奧尼爾ENC1105,ENC3250,REL2210

40歐文ENC1105,COP3250,ISO4250

70伯恩COP2250,ENC3250,COP3530

[, ,,,,,,,]

爲什麼我得到這一行?

[,,,,,,,,,]

感謝您的幫助!

+1

請說明一下,你是什麼意思*「我想刪除[,]返回null」*。 – lexicore

+0

當然...正確地遍歷** list **數組。嘗試使用** for for循環。 – DevilsHnd

+0

輸出如下: 20亞當斯\t WNC1105,ENC3250,REL2210 70伯恩\t COP2250,ENC3250,COP3530 10布拉加\t ENC1105,ENC3250,ISO4250 40歐文\t ENC1105,COP3250,ISO4250 60 Giron的\t COP1210,COP3337, COP3530 50的Gracia \t COP2250,COP3250,COP4250 30瓊斯\t COP1210,COP3337,COP3530 45奧尼爾\t ENC1105,ENC3250,REL2210 10史密斯\t COP2250,COP3250,COP4250 20威爾遜\t WNC1105,ENC3250,RE L2210 [,,,,,,,,] – Mario

回答

0

當你做System.out.println(list);你只需使用ArrayList.toString()方法的默認實現,它返回列表中逗號和空格分隔的[]括號中的值。 您有三個選項有:

  1. 迭代通過你自己的列表,並分別打印每個學生(只要它有它的toString()方法實現

  2. 或者你可以使用的replaceAll()進行。你現在從list.toString()

首先選擇具有字符串是一般較好,因爲通常情況下"[" "]" ", "可以是列表的Elemen內有效字符並且不能被替換。 但是,對於小的情況下,當你肯定會有Student name, id or course沒有這樣的字符,你可以做到這一點。

0

當您使用System.out.println(list)您正在使用的列表的默認實現打印的東西到控制檯。

默認實現迭代您的列表並調用每個對象的toString。在學生課中重寫該功能是個好主意。但你只是回來「」。默認實現創建一個新的String。它以「[」開頭。它隨後將清單內的每個對象的toString方法的返回值(由分隔「」)。它以「]」結尾。你的toString方法返回一個空的String「」。這意味着默認的實現將創建下列:

[,,,,,,,,,]

現在你可以改變你的toString方法的返回值:

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

比你會得到下面的輸出:

[50格拉西亞COP2250,COP3250,COP4250,30瓊斯COP1210,CO P3337,COP3530,10 Smith COP2250,COP3250,COP4250,20威爾遜WNC1105,ENC3250,REL2210,10布拉加ENC1105,ENC3250,ISO4250,20 Adams WNC1105,ENC3250,REL2210,60 Giron COP1210,COP3337,COP3530,45 O'Neal ENC1105, ENC3250,REL2210,40歐文ENC1105,COP3250,ISO4250,70伯恩COP2250,ENC3250,COP3530]

如果你不想讓你可以編寫自己的名單,從ArrayList中延伸commatars和支架,或者你迭代你的列表,而不是打印它。

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

然後你會得到下面的輸出:

50格拉西亞COP2250,COP3250,COP4250

30瓊斯COP1210,COP3337,COP3530

10史密斯COP2250,COP3250,COP4250

20 Wilson WNC1105,ENC3250,REL2210

10布拉加ENC1105,ENC3250,ISO4250

20亞當斯WNC1105,ENC3250,REL2210

60 Giron的COP1210,COP3337,COP3530

45奧尼爾ENC1105,ENC3250,REL2210

40歐文ENC1105,COP3250,ISO4250

70伯恩COP2250,ENC3250,COP3530

0

如果列出你列表 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 
+0

我知道我需要一個for循環某處只是沒有點擊,我怪週末!再次感謝這工作很好。 – Mario