0

Trying to print a list nameOfList which has some arrayList1, arrayList2, someObj but everytime I print it, I get [SomeTO@24a34c78, SomeTO@141e2fa].

I've already tried printing it as nameList.toString()

List<SomeTO> nameOfList = new ArrayList<SomeTO>();

Ini ini = new Ini(new FileReader(Class2.someconstantvalue));
for (String stringName: ini.keySet()) {
   Section section = ini.get(stringName);

   ArrayList<String> arrayList1 = Arrays.asList(section.get(Class2.someconstants).split(","))
   ArrayList<String> arrayList2 = Arrays.asList(section.get(Class2.moreconstants).split(","))

   SomeTO someObj = new SomeTO()
   someObj.setsection(StringName)
   someObj.setArrayList1(arrayList1)
   someObj.setArrayList2(arrayList2)
   //some more code like this
   nameOfList.add(someObj)
}
   //I want to print nameOfList here before I return it so I can be sure its right.
   return nameOfList
  
asfas
  • 21
  • 5
  • 1
    Does your `SomeTO` class implement `toString()`? – Daniel Jun 21 '21 at 19:56
  • @Daniel no that class `SomeTO implements Cloneable {}` – asfas Jun 21 '21 at 19:58
  • 2
    You can either implement toString(), or if this is just for debug you could do `println nameOfList*.section` assuming every item in the list is a `SomeTO` and there is a field named `section` (as appears to be the case from your example) – Daniel Jun 21 '21 at 19:59
  • @Daniel Would I be able to write `nameOfList*.section` outside the for loop? – asfas Jun 21 '21 at 20:07
  • Yes. Also a heads up that this is probably just an error in your example code here, but you're not adding the `someObj` to your list until outside the for loop, which is likely not what you want. – Daniel Jun 21 '21 at 20:13
  • @Daniel Sorry! I misstyped. I have edited the question so that I add `someObj` to my list inside the for loop. How would I do it now? – asfas Jun 21 '21 at 20:22
  • You would still use `*.section`. Try it and update your question with more details if it doesn't work the way you need. – Daniel Jun 21 '21 at 20:25
  • @Daniel weirdly enough, `nameOfList*.section` only prints out the content of `ini.get(stringName);`. I want to print out everything like `someObj` stuff and the 2 arraylists – asfas Jun 21 '21 at 20:40
  • Then as discussed below, implement `toString()`. – Daniel Jun 21 '21 at 21:31
  • you've tagged the question with `[groovy]`, so if `SomeTO` is a class under your control, you can also give the class the [@ToString](http://docs.groovy-lang.org/2.4.9/html/gapi/groovy/transform/ToString.html) annotation and groovy will try and implement `.toString()` for you. – thehole Jun 22 '21 at 14:02

2 Answers2

1

You have to override the Object.toString() method in your SomeTO class

class SomeTO {

// your methods

   public String toString() {
       return "some information";
   }
}

It's a good practice to return the fields values of your class inside the toString() method.

You can read more about the topic here.

Emerson Pardo
  • 151
  • 1
  • 9
  • I'm confused. What do I write in "return..."? – asfas Jun 21 '21 at 20:23
  • You can put, for example, `return this.getSection();` – Emerson Pardo Jun 21 '21 at 20:32
  • But what if I want to return the whole list? if I do .getSection() wouldn't it only return the section? – asfas Jun 21 '21 at 20:47
  • Just concatenate what you want to be present when you print. See here for some more examples: https://www.baeldung.com/java-tostring – Emerson Pardo Jun 21 '21 at 21:01
  • You can return any string you want. You could return `${section}\n${arrayList1}\n${arrayList2}` if you wanted, though that's probably a bit verbose. Implement `toString()` with something simple, test it, then add to it until it's working the way you want. – Daniel Jun 21 '21 at 21:32
0

What you are currently seeing is the address in order to see the value ,override toString() method and return whatever value you want to see being returned . @override public String toString(){ return “value”;}

  • It is not the address. It is the `Object.hashCode()` implementation and it varies depending on the JVM implementation. See this answer for a discussion about the Object.hashCode implementation: https://stackoverflow.com/questions/1516843/java-object-hashcode-result-constant-across-all-jvms-systems – Emerson Pardo Jun 21 '21 at 20:40