2015-12-02 87 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 * /]''或''列表''。 –