Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 676033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:53:01+00:00 2026-05-14T00:53:01+00:00

I have a task, which I was able to do with the use of

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:53:01+00:00Added an answer on May 14, 2026 at 12:53 am

    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()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.