使用集合重写数组

发布于 2024-08-25 03:54:59 字数 1215 浏览 2 评论 0原文

我有一个任务,我可以使用最简单的方法 - 数组来完成该任务。现在我想更进一步,使用一些更复杂的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

琉璃繁缕 2024-09-01 03:54:59

您可以使用 Set 作为集合防止重复元素。

如果您尝试添加已经存在的元素,它将返回false

Set<Block> set = new HashSet<Block>();

boolean added = set.add( k1 );
if( added ) {
    System.out.println("Added 1 block.");
} else {
   System.out.println("Tower already contains this block.");
}

要知道集合是否已包含元素,类应正确实现 equals() 方法。

要提供描述消息,您可以覆盖 toString() 方法并迭代集合:

 for( Block b : set ) {
     System.out.println( b ); // invokes b.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.

Set<Block> set = new HashSet<Block>();

boolean added = set.add( k1 );
if( added ) {
    System.out.println("Added 1 block.");
} else {
   System.out.println("Tower already contains this block.");
}

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:

 for( Block b : set ) {
     System.out.println( b ); // invokes b.toString()
}
西瑶 2024-09-01 03:54:59

如果您的 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.

留一抹残留的笑 2024-09-01 03:54:59

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文