Tuesday, 15 September 2015

java - How to display a contents from an Array List in a string? -


I am starting to use Java and I do not know how the system outputs content from my array list To make a private void () (Room City Center, boot, Tesco, library, home; // city center to make room = new room ("city center") Shoes "= new room (" drug store in boot ", material); Tesco = new room (" Tesco's ", material) Library = new room ("in city library", content); home = new room ("at home", content);

Note where 'content' says Where I am trying to display objects in the room.

This is where I started my array list

  new = milk (new) "); Paracetamol = new object (" paracetamol "); book = new objects (" book "); money = new object (" money "); tesco.addObjects (milk); bo Ots.addObjects (paracetamol); library.addObjects (book); home.addObjects (money);  

The array list itself is in a different class and looks like this

  Public Array List & Lieutenant; Objects & gt; material;  

So what am I trying to do, the system prints the contents of each room. Any help is welcomed and appreciated :)

First you have toString your "Objects" (), Which represent the contents of your room, thus you can give a friendly description for each "objects".

Inside your object class:

  @Override public string toString () {return "Object:" + object name; After this, you can override the toString () method of your room class so that you provide your details of the room, in which case you are in your "materials" arrelist. You want to do all the printing by printing  

You can do the following in your cell orbit:

  @ Override to public string toString (return) "Content in the room" + Content String (); }  

An ArrayList has a toString method that will call the toString () method for each element in the list. In our case, each element is an "objects", which we only override the string () method, so that's what we really wanted.

Now when you call:

  System.out.println (Tesco)  

This output will be:

In-room content: Object: Milk

1 comment:

  1. Rb88 Promo Code 2021 - £20 Matched Free Bet
    › rb88-promo-code-g › rb88-promo-code-g Rb88 Promo Code rb88 – Bet £20 Matched Free Bet and get a matched free bet worth up to £20! The Rb88 betting site is dafabet a high-quality site that is easy to sign 메리트카지노 up. Read the review.

    ReplyDelete