Sorted ArrayList not displaying?

前端 未结 1 564
滥情空心
滥情空心 2021-01-28 17:14
ArrayList  cdcollection = new ArrayList();

private void initButtonActionPerformed(java.awt.event.ActionEvent evt) {
    Collections.addAll(cdcollection, \         


        
相关标签:
1条回答
  • 2021-01-28 17:29

    You have defined collection as a string like String collection = "";. There is no method called get(j) in String class. Try to use your arraylist object cdcollection. Also take of the creation of arraylist. You have done like ArrayList <String> cdcollection = new ArrayList();, try to use generics both the sides like ArrayList <String> cdcollection = new ArrayList<String>();, otherwise you might get unnecessary warnings.

    EDIT:

    public static void main(String[] args){
        ArrayList<String> cdcollection = new ArrayList();
        Collections.addAll(cdcollection, "renier\n", "mert\n", "rain\n",
                "mylen", "dreb\n");
    
        String title1 = "Original Order\n\n";
        String title2 = "Sorted Order\n\n";
    
        String collection = "";
        for (int i = 0; i < cdcollection.size(); i++) {
            collection = collection + cdcollection.get(i);
        }
        System.out.println(title1 + collection);
    
        Collections.sort(cdcollection);
        System.out.println(cdcollection);
    }
    
    0 讨论(0)
提交回复
热议问题