2016-11-10 122 views
0

編寫一種方法來返回列表中最常出現的玩具,另一種方法是通過計數來分類玩具。使用ArrayList對Java中最高最低的對象排序?

這是我的代碼

import java.util.ArrayList; 

public class ToyStore { 
    private ArrayList<Toy> toyList; 

    public ToyStore() { 
    } 

    public void loadToys(String toys) { 
     toyList = new ArrayList<Toy>(); 
     for (String item : toys.split(" ")) { 
      Toy t = getThatToy(item); 
      if (t == null) { 
       toyList.add(new Toy(item)); 
      } else { 
       t.setCount(t.getCount() + 1); 
      } 
     } 
    } 

    public Toy getThatToy(String nm) { 
     for (Toy item : toyList) { 
      if (item.getName().equals(nm)) { 
       return item; 
      } 
     } 
     return null; 
    } 

    public String getMostFrequentToy() { 
     int position = 0; 
     int maximum = Integer.MIN_VALUE; 
     for (int i = toyList.size() - 1; i >= 0; i--) { 
      if (toyList.get(i).getCount() > maximum) 
       maximum = toyList.get(i).getCount(); 
      position = i; 
     } 
     return toyList.get(position).getName(); 
    } 

    public void sortToysByCount() { 
     ArrayList<Toy> t = new ArrayList<Toy>(); 
     int count = 0; 
     int size = toyList.size(); 

     for (int i = size; i > 0; i--) { 
      t.add(new Toy(getMostFrequentToy())); 
      t.get(count).setCount(getThatToy(getMostFrequentToy()).getCount()); 
      toyList.remove(getThatToy(getMostFrequentToy())); 
      count++; 
     } 

     toyList = t; 
    } 

    public String toString() { 
     return toyList + "" + "\n" + "max == " + getMostFrequentToy(); 
    } 
} 

這是我關心

public void sortToysByCount() { 
    ArrayList<Toy> t = new ArrayList<Toy>(); 
    int count = 0; 
    int size = toyList.size(); 

    for (int i = size; i > 0; i--) { 
     t.add(new Toy(getMostFrequentToy())); 
     t.get(count).setCount(getThatToy(getMostFrequentToy()).getCount()); 
     toyList.remove(getThatToy(getMostFrequentToy())); 
     count++; 
    } 

    toyList = t; 
} 

這裏是我的輸出

[sorry 4, bat 1, train 2, teddy 2, ball 2] 

這裏的方法就是我想要的

[sorry 4, train 2, teddy 2, ball 2, bat 1]; 

我的代碼有什麼問題?我該怎麼做?

+5

用調試器遍歷你的代碼並找出它。家庭作業是爲了通過試驗和錯誤來教導,如果我們爲你做這件事有什麼意義? –

+0

它看起來像一個家庭作業。這是我們的[關於作業]的政策(http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems)。 –

回答

1

的問題是在你的getMostFrequentToy()方法:

更換

 if (toyList.get(i).getCount() > maximum) 
      maximum = toyList.get(i).getCount(); 
     position = i; 

 if (toyList.get(i).getCount() > maximum) { 
      maximum = toyList.get(i).getCount(); 
      position = i; 
     } 

,因爲你想要得到的是對應於最大的位置。

0

你的代碼有一些無效率。每次您撥打getMostFrequentToy()時,都會遍歷整個列表,這可能很好,因爲您不斷刪除對象,但實際上並不需要爲列表中已存在的對象製作new Toy對象。

所以,這是「更好的」,但仍然不確定你需要getThatToy,當你應該已經知道哪一個是最頻繁的。

String frequent; 
for (int i = size; i > 0; i--) { 
    frequent = getMostFrequentToy(); 
    t.add(new Toy(frequent)); 
    t.get(count).setCount(getThatToy(frequent).getCount()); 
    toyList.remove(getThatToy(frequent)); 
    count++; 
} 

不管怎麼說,我想說明要求你歸還玩具對象,而不是它的名稱。

這很簡單,只需跟蹤最大計數。現在

public Toy getMostFrequentToy() { 
    Toy mostFrequent = null; 
    int maximum = Integer.MIN_VALUE; 

    for (Toy t : toyList) { 
     if (t.getCount() > maximum) 
      mostFrequent = t; 
    } 
    return t; 
} 

,上面的代碼可以成爲

public void sortToysByCount() { 
    ArrayList<Toy> t = new ArrayList<Toy>(); 
    // int count = 0; 
    int size = toyList.size(); 

    Toy frequent; 
    for (int i = size; i > 0; i--) { 
     frequent = getMostFrequentToy(); 
     t.add(frequent); 
     // t.get(count).setCount(frequent.getCount()); // Not sure about this 
     toyList.remove(frequent); 
     // count++; 
    } 

    toyList.clear(); 
    toyList.addAll(t); 
} 

現實,不過,當你要排序,你真的應該看看如何create a Comparator for your Toy objects