使用集合重写数组
我有一个任务,我可以使用最简单的方法 - 数组来完成该任务。现在我想更进一步,使用一些更复杂的 java 功能(如集合)重做它,但我从未使用过比二维矩阵更复杂的东西。我应该看什么以及如何开始。塔应该成为一个收藏品吗?任务是:
我们有两个类 - 塔楼和街区。塔是用方块建造的。 这里是用于测试的示例代码:
Block k1=new Block("yellow",1,5,4);
Block k2=new Block("blue",2,2,6);
Block k3=new Block("green",3,4,2);
Block k4=new Block("yellow",1,5,4);
Tower tower=new Tower();
tower.add(k1,k2,k3);
"Added 3 blocks."
System.out.println(tower);
"block: green, base: 4cm x 3cm, thicknes: 2 cm
block: blue, base: 6cm x 2cm, thicknes: 2 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm"
tower.add(k2);
"Tower already contains this block."
tower.add(k4);
"Added 1 block."
System.out.println(tower);
"block: green, base: 4cm x 3cm, thicknes: 2 cm
block: blue, base: 6cm x 2cm, thicknes: 2 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm"
tower.delete(k1);
"Deleted 1 block"
tower.delete(k1);
"Block not in tower"
System.out.println(tower);
"block: blue, base: 6cm x 2cm, thicknes: 2 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm"
假设我将 Tower 视为块的集合。如何在整个集合中搜索特定块?或者我应该使用其他接口?
I have a task, which I was able to do with the use of simplest methods - arrays. Now I'd like to go further and redo it using some more complicated java features like collections, but I've never used anything more complicated than 2d matrix. What should I look at and how to start with it. Should Tower become a Collection ? And here's the task :
We have two classes - Tower and Block. Towers are built from Blocks.
Ande here's sample code for testing:
Block k1=new Block("yellow",1,5,4);
Block k2=new Block("blue",2,2,6);
Block k3=new Block("green",3,4,2);
Block k4=new Block("yellow",1,5,4);
Tower tower=new Tower();
tower.add(k1,k2,k3);
"Added 3 blocks."
System.out.println(tower);
"block: green, base: 4cm x 3cm, thicknes: 2 cm
block: blue, base: 6cm x 2cm, thicknes: 2 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm"
tower.add(k2);
"Tower already contains this block."
tower.add(k4);
"Added 1 block."
System.out.println(tower);
"block: green, base: 4cm x 3cm, thicknes: 2 cm
block: blue, base: 6cm x 2cm, thicknes: 2 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm"
tower.delete(k1);
"Deleted 1 block"
tower.delete(k1);
"Block not in tower"
System.out.println(tower);
"block: blue, base: 6cm x 2cm, thicknes: 2 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm
block: yellow, base: 5cm x 4cm, thicknes: 1 cm"
Let's say I will treat Tower as a collection of blocks. How to perform search for specific block among whole collection ? Or should I use other interface ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Set 作为集合防止重复元素。
如果您尝试添加已经存在的元素,它将返回
false
。要知道集合是否已包含元素,类应正确实现 equals() 方法。
要提供描述消息,您可以覆盖 toString() 方法并迭代集合:
You can use as collection a Set which prevents duplicate elements.
It will return
false
if you try to add an element which is already present.To know if the set already contains an element the class should implement correctly the equals() method.
To provide the description messages you may override the toString() method and iterate the set:
如果您的 Tower 类有一个作为集合的成员变量 (a Set 似乎适合您禁止重复的需求),那么您可以提供与数组实现相同的访问器。
然后,您将保留塔和砖块的域模型。您还可以向客户端隐藏内部数据结构的实现,并且不需要完全实现 Collections 接口,其中可能包括您实际上不需要的方法。
If your Tower class had a member variable that was a collection (a Set would seem to fit your needs of disallowing duplicates) then you could provide the same accessors you have with your array implementation.
You then would preserving your domain model of Towers and Bricks. You would also hide the implementation of the internal data structure from clients, and you wouldn't need to fully implement a Collections interface, which may include methods you don't actually need.
您可以使用 Set。
使用 Set,您不需要搜索重复项(我假设这就是需要搜索功能的原因),因为使用 add(E) 方法会返回一个标志,指示是否存在重复项。
You could use a Set.
With a Set, you wouldn't neeed to search for duplicates (I'm assuming that's why want a search function), since using the add(E) method returns a flag indicating whether there is a duplicate.