2015-12-02 92 views
1

我希望我的標題能夠更簡潔,但我不太清楚如何描述我想要做的事情。我有一個列表,其中包含我創建的一個類的一些對象。每個對象都有一些屬性,可以被一些getter和setter訪問。我想爲每個屬性創建一個列表,獲取每個對象的屬性並將它們放入它們各自的列表中。從列表中的對象的屬性創建列表

我目前做這與下面的代碼:

ArrayList<Integer> counts = new ArrayList<Integer>(); 
ArrayList<String> colors = new ArrayList<String>(); 
ArrayList<String> shapes = new ArrayList<String>(); 
ArrayList<String> fills = new ArrayList<String>(); 

for (Card card: cards) { 
    counts.add(card.getCount()); 
    colors.add(card.getColor()); 
    shapes.add(card.getShape()); 
    fills.add(card.getFill()); 
} 

但我不知道是否有一個更短的,也許更好,方式做到這一點?或者我是否已經做到了「正確」?謝謝!

+2

我很好奇:你爲什麼要這麼做?爲什麼在多個列表中存儲屬性分開比在一個列表中存儲包含屬性的對象更好? – gefei

+0

也許有一個更好的解決方案,我想要做什麼。我想檢查每個屬性列表是包含完全不同的元素還是完全相同的元素。 – tobloef

+0

看起來你需要開發[設置遊戲](https://en.wikipedia.org/wiki/Set_(遊戲))作爲功課... –

回答

1

你的方式看起來不錯。如果您使用Java 8 Streams,則可以使用較少的代碼行編寫代碼,但這需要在cards列表上執行4次迭代,而不是您當前擁有的單次迭代。

例如,創建計數的列表,你可以寫:

List<Integer> counts = cards.stream().map(Card::getCount).collect(Collectors.toList()); 
+0

這只是我一直在尋找的東西。我可能不會使用此代碼,因爲它不太容易閱讀,但很高興知道您可以以其他方式進行操作。 – tobloef

+2

@TobLoef我建議你閱讀Streams API和lambda表達式。有一天它可能對你有用,所以習慣這種語法是個好主意。一旦你習慣了它,你可能會改變主意,看它是否易於閱讀。 – Eran

+0

肯定會做。謝謝! – tobloef

1

如果你正在開發一個set game,這裏是檢查並完成卡套的解決方案:

卡。 Java的:

import java.util.Locale; 

public final class Card { 
    public static void main(String[] args) { 
     Card a = new Card(Count.ONE, Color.RED, Fill.STRIPED, Shape.DIAMOND); 
     Card b = new Card(Count.TWO, Color.RED, Fill.SOLID, Shape.DIAMOND); 
     Card c = completeSet(a, b); 
     System.out.println(c); 
    } 

    public int count; 
    public int color; 
    public int fill; 
    public int shape; 

    public Card() { 

    } 

    public Card(Count count, Color color, Fill fill, Shape shape) { 
     setCount(count); 
     setColor(color); 
     setFill(fill); 
     setShape(shape); 
    } 

    // getters and setters to operate with the attribute enumerations 
    public Count getCount() { 
     return Count.values()[count]; 
    } 

    public Color getColor() { 
     return Color.values()[color]; 
    } 

    public Shape getShape() { 
     return Shape.values()[shape]; 
    } 

    public Fill getFill() { 
     return Fill.values()[fill]; 
    } 

    public void setCount(Count count) { 
     this.count = count.ordinal(); 
    } 

    public void setColor(Color color) { 
     this.color = color.ordinal(); 
    } 

    public void setShape(Shape shape) { 
     this.shape = shape.ordinal(); 
    } 

    public void setFill(Fill fill) { 
     this.fill = fill.ordinal(); 
    } 

    public static int completeAttribute(int a, int b) { 
     if (a == b) { 
      // attribute for each same 
      return a; 
     } else { 
      // attribute for each different 
      int c; 
      for (c = 0; c < 3; c++) { 
       if (c != a && c != b) { 
        break; 
       } 
      } 
      return c; 
     } 
    } 

    public static Card completeSet(Card a, Card b) { 
     // determine missing card to make a set 
     Card result = new Card(); 
     result.count = completeAttribute(a.count, b.count); 
     result.color = completeAttribute(a.color, b.color); 
     result.shape = completeAttribute(a.shape, b.shape); 
     result.fill = completeAttribute(a.fill, b.fill); 
     return result; 
    } 

    public static boolean isSet(Card a, Card b, Card c) { 
     // check if it is a set by completing it 
     return completeSet(a, b).equals(c); 
    } 

    @Override 
    public boolean equals(Object that) { 
     // currently unused 
     if (this == that) { 
      return true; 
     } 
     return that != null && that instanceof Card && this.equals((Card) that); 
    } 

    public boolean equals(Card that) { 
     if (this == that) { 
      return true; 
     } 
     return that != null 
       && this.color == that.color 
       && this.count == that.count 
       && this.fill == that.fill 
       && this.shape == that.shape; 

    } 

    @Override 
    public int hashCode() { 
     // currently unused 
     int result = count; 
     result = 31 * result + color; 
     result = 31 * result + shape; 
     result = 31 * result + fill; 
     return result; 
    } 

    @Override 
    public String toString() { 
     // pretty print attributes 
     String result = getCount() + " " + getColor() + " " + getFill() + " " + getShape(); 
     result = result.toLowerCase(Locale.ROOT); 
     if(getCount() != Count.ONE) { 
      result += "s"; 
     } 
     return result; 
    } 

    // enumerations for pretty names 
    public enum Count { 
     ONE, 
     TWO, 
     THREE 
    } 

    public enum Color { 
     RED, 
     GREEN, 
     VIOLET 
    } 

    public enum Shape { 
     ELLIPSE, 
     DIAMOND, 
     TWIRL 
    } 

    public enum Fill { 
     OPEN, 
     STRIPED, 
     SOLID 
    } 
} 

輸出:three red open diamonds

+0

儘管你正確地猜測我正在開發與SET有關的東西,但這並不能真正回答我的問題。感謝您發佈代碼,但我會看看您的實現,以瞭解它與我的做法有何不同。 – tobloef

+0

@TobLoef是的,你是對的。這實際上不是你的問題的答案。但將屬性保留爲原語應該讓你瞭解如何最容易地處理它們。你的''''''''''屬性的''列表''可以被定義爲:''int [/ * 4 * /] [/ * cards count * /]''或''列表''。 –