2017-03-27 36 views
0

我在我的程序中創建了我自己的LinkedList(不是java.util)的卡片。對作爲整數對的對象進行排序

每個卡是由2整數對象 - 第一個是一個卡(從1到13)的值,第二個是一個卡(從0到3)的顏色。

名單是隨機產生的,並在創建列表時的值是0

現在我想升級我加入卡的方法對它們進行排序,同時增加。 首先按價值,然後按顏色

這裏是我的代碼看起來像現在:

public class Lista { 
private Element pocz; //start 
public int rozmiar; 

public Lista() { 
    boolean zrobione = false; 
    while (zrobione != true) { 
     Karta karta = new Karta(); 
     if (karta.getWartosc() == 0) { 
      zrobione = true; 
     } else { 
      this.dodaj(karta); 
     } 
    } 
} 

public void dodaj(Karta k) { 
    if (pocz == null) { 
     pocz = new Element(k); 
    } 

    Element pom = new Element(k); 
    Element obecny = pocz; 
    if (obecny != null) { 
     while (obecny.getNext() != null) { 
      obecny = obecny.getNext(); 
     } 
     obecny.setNext(pom); 
    } 
    rozmiar++; 
} 

我一直在想一個解決方案,頗有幾分喜歡的東西來到了:

public void dodaj(Karta k){ 
    if(rozmiar == 0) { 
     if (pocz == null) { 
      pocz = new Element(k); 
     } 

     Element pom = new Element(k); 
     Element obecny = pocz; 
     if (obecny != null) { 
      while (obecny.getNext() != null) { 
       obecny = obecny.getNext(); 
      } 
      obecny.setNext(pom); 
     } 
     rozmiar++; 
    } 
    else{ 
     Element pom = new Element(k); 
     Element obecny = pocz; 
     boolean znalezione = false; 
     if(obecny != null && !znalezione){ 
      while(obecny.getNext() != null && !znalezione){ 
       if(obecny.getKarta().wartosc < obecny.getNext().getKarta().wartosc) { 
        obecny.setNext(pom); 
        znalezione = true; 
       } 
       else if(obecny.getKarta().wartosc == obecny.getNext().getKarta().wartosc){ 
        if(obecny.getKarta().kolor < obecny.getNext().getKarta().kolor){ 
         obecny.setNext(pom); 
         znalezione = true; 
        } 
        else if(obecny.getKarta().kolor == obecny.getNext().getKarta().kolor){ 
         obecny.setNext(pom); 
         znalezione = true; 
        } 
        else{ 
         obecny = obecny.getNext(); 
         obecny.setNext(pom); 
         znalezione = true; 
        } 
       } 
       else{ 
        obecny = obecny.getNext(); 
        obecny.setNext(pom); 
        znalezione = true; 
       } 
      } 
     } 
     rozmiar++; 
    } 
} 

但這種方法使我的清單隻包含2張卡片,我不確定它是否可以使用... 對不起英文名稱的變量和類別。

什麼,我想實現舉例:

Before sorting 
11 2 
5 3 
2 3 
13 0 
1 2 
5 2 
1 1 
5 1 

After sorting 
1 1 
1 2 
2 3 
5 1 
5 2 
5 3 
11 2 
13 0 

rozmiar是List的大小
wartosc是卡片的價值
kolor是卡
dodaj意味着色彩 「添加」

回答

0

在不知道每種方法/字段含義的情況下,很難理解代碼。但一般來說,使你的Card類實現Comparable接口。並定義適當的compareTo方法。這將是繼

public class Card implements Comparable<Card> { 
    private int value; 
    private int color; 

    //... 

    @Override 
    public int compareTo(@NotNull Card o) { 
     final int valueResult = Integer.compare(value, o.value); 
     if (valueResult != 0) { 
      return valueResult; 
     } 
     return Integer.compare(color, o.color); 
    } 
} 

簡單得多之後,你可以通過調用card1.compareTo(card2)比較兩張牌。

+0

問題是我的任務是要做到這一點,而不使用的compareTo。我知道,我知道它的愚蠢,但... – Szouter

0

問題是我的任務是不使用compareTo來做到這一點。

好吧,如果你需要做的是編程,無需使用Java排序方法,使用穩定的排序 =算法不重新安排具有平等的關鍵要素,如合併排序或插入排序,有利於逐步插入。 這種情況下,首先按顏色對數組進行排序,然後按的值。在這裏,一個穩定的排序將留下價值等於顏色

https://en.wikipedia.org/wiki/Category:Stable_sorts

示例僞代碼:

int[][] cards; 

cards = mergeSort(cards, cards[1]); 
cards = mergeSort(cards, cards[0]); 

Object[] mergeSort(Object[] array, int[] keys) {...} 
0

謝謝你們的努力,但我想我要做的就是寫,將確定的,其中在LinkedList的新生成的卡應該的方法被添加。事情是我提出第二個方法「dodaj」 ......

+0

你必須把它寫到問題部分(用這個信息更新它)。因爲它不是你的問題的答案。這只是一個額外的信息。 –

0

我的建議對你來說是:

  • 如果你使用Java語言的使用命名約定,爲您的代碼。它使您的代碼對其他開發人員來說是可以理解
  • 使用Java 8收集操作。它有很多簡單收集例程的功能。

我創建小的演示:

@Getter 
@NoArgsConstructor 
@AllArgsConstructor 
class Card { 
    private int value; // 1-13 
    private int colour; // 1-3 

    @Override 
    public String toString() { 
     return String.valueOf(value + " " + colour); 
    } 
} 

public class ComparatorDemo { 
    public static void main(String[] args) { 
     final List<Card> cards = Arrays.asList(
       new Card(11, 2), 
       new Card(5, 3), 
       new Card(2, 3), 
       new Card(13, 0), 
       new Card(1, 2), 
       new Card(5, 2), 
       new Card(1, 1), 
       new Card(5, 1)); 

     final Function<Card, Integer> byValue = Card::getValue; 
     final Function<Card, Integer> byColour = Card::getColour; 

     System.out.println("Before sorting"); 
     cards.forEach(System.out::println); 

     List<Card> sortedList = cards.stream() 
       .sorted(Comparator.comparing(byValue).thenComparing(byColour)) 
       .collect(Collectors.toList()); 

     System.out.println(); 
     System.out.println("After sorting"); 
     sortedList 
       .forEach(System.out::println); 
    } 
} 

Card類是什麼,你叫Kartka。我使用Lombok framework註釋@Getter來省略冗餘代碼。
此外,測試數據與您在問題中提到的值完全相同。

輸出爲

Before sorting 
11 2 
5 3 
2 3 
13 0 
1 2 
5 2 
1 1 
5 1 

After sorting 
1 1 
1 2 
2 3 
5 1 
5 2 
5 3 
11 2 
13 0