1

Hello How do I convert in a ArrayList<> something to String I will show you my code I am really bad a at explaining stuff sorry:

ArrayList<HerniObjekt> batoh = new ArrayList<>();

public void pridej(HerniObjekt objekt)
{
    batoh.add(objekt);
}

public void vypis()
{
    JOptionPane.showMessageDialog(null, "Mas v Inevtari "+batoh.);
}

And I would like to convert the HerniObject to String how do I do that? thanks for your help.

2
  • 1
    And what does HerniObekt look like? Which String you would like to get from each object? And finally, what have you tried? Commented Aug 16, 2013 at 9:32
  • HenriObject is a class where all the the other classes like NPC abstract from it the class HerniObject: package javagame; import java.awt.Graphics; public abstract class HerniObjekt { int y; int x; int smerx; int smery; int rychlost; public abstract void krok(); public abstract void vykresli(Graphics g); } and I tried HenriObject.toString Commented Aug 16, 2013 at 9:34

6 Answers 6

3

You need to build a string by iterating through the array and adding each element to a StringBuilder

e.g. without specifying a separator....

StringBuilder sb = new StringBuilder();
for (HerniObjekt h : batoh) {
   sb.append(h);
}

Alternatively, Guava has a Joiner class.

Joiner joiner = Joiner.on("; ").skipNulls();
 . . .
return joiner.join("Harry", null, "Ron", "Hermione");

In either case you should override toString() in your HerniObjekt class

Sign up to request clarification or add additional context in comments.

1 Comment

How do I do that I new to Java? sorry
0

In your class HerniObjekt, you can override the method public String toString(){} so it returns what you want to output in String

Comments

0

override toString() method in HerniObject ,then foreach add to arraylist.

Comments

0

For a list you could use HTML markup too.

StringBuilder sb = new StringBuilder();
sb.append("<html><p>Mas v Inevtari</p><ul>");
for (HerniObjekt herni : batoh) {
    sb.append("<li>").append(herni).append("</li>");
}
sb.append("</ul>");

JOptionPane.showMessageDialog(null, sb.toString());

The above append(herni) will use toString().

Comments

0

get the HerniObjekt list object and assign it to HerniObjekt object in for-each loop

i.e.

StringBuilder strBuilder = new StringBuilder();
for(HerniObjekt herni : batoh){
  strBuilder.append(herni);
}

Append the HerniObjekt object value to a StringBuilder object shown above.

Comments

0
 class HerniObjekt{
      int id;

      @Override // Override  this method with meaningful
      public String toString() {
           return "Id=" + this.id ;
      }
}

ArrayList<HerniObjekt> batoh = new ArrayList<>();
StringBuilder strBuilder = new StringBuilder();
for (HerniObjekt object: batoh ) {
    strBuilder.append(object.toString());
}

System.out.println(strBuilder.toString());

or

System.out.println(batoh.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.