title: (Java) How to return objects from a list as a string or array of strings?
body: <p><strong>EDIT: I have uploaded both full class files and a tester file to dropbox in case you want to try it out yourselves:</strong> <a href="https://www.dropbox.com/sh/03qsecq3bd55w78/AAA_PT_BczNGl7cTZO_Zf_qUa?dl=0" rel="nofollow">https://www.dropbox.com/sh/03qsecq3bd55w78/AAA_PT_BczNGl7cTZO_Zf_qUa?dl=0</a></p>
  
  <p>First off, sorry if this is a very simple question - I've only just started working with java and I don't have a lot of programming experience at all.</p>
  
  <p>So I have two classes: Posada and Camino. Posada contains a list of Camino objects. I need to override Posada's toString method so that it returns a string containing all of the toString methods of the Camino objects its list contains, like so:</p>
  
  <p>Posada:</p>
  
  <pre><code>public class Posada {
  
      /*other attributes*/
      private List&lt;Camino&gt; listCaminos;   
  
      /*Getters*/
      public List&lt;Camino&gt; getLisaCaminos() {
          return listCaminos;
      }
  
      /*Builder*/
      public Posada(String nombre, int engRecuperada) {
          /*Other attributes*/
          this.listaCaminos = new ArrayList&lt;Camino&gt;();
      }
  
      /*Methods*/
      public String getStringToReturn(){
          String[] arrayCaminos = listCaminos.toArray(new
          String[listCaminos.size()]);
  
          String string = "";
          string += /*Some other attributes that go before the Camino objects
          and work fine*/;
  
          for(int i = 0; i &lt; listCaminos.size(); i++) {
              string += ", " + arrayCaminos[i];
          }
  
          return string;
      }
  
      /*toString*/
      @Override
      public String toString() {
          return getStringToReturn(); //failure
      }
  
  }
  </code></pre>
  
  <p>Camino</p>
  
  <pre><code>public class Camino {
  
      /*Attributes*/
      private Posada origin;
      private Posada destination;
      private int cost;
  
      private static int costTotal;
  
      /*Contructor*/
      public Camino(Posada origin, Posada destination, int cost){
          this.origin = origin;
          this.destination = destination;
          this.cost = cost;
          Camino.costTotal = Camino.costTotal + cost;
  
      }
  
      /*Getters*/
      public int getCost() {
          return cost;
      }
  
      public Posada getDestination() {
          return destination;
      }
  
      public Posada getOrigin() {
          return origin;
      }
  
      /*Methods. Irrelevant*/
  
  
      /*toString*/
      @Override
      public String toString() {
          return "(" + getOrigin() + "--" + getCost() + "--&gt;" +
          getDestination()+ ")";
      }
  
  }
  </code></pre>
  
  <p>So the expected result when printing Posada should be something like "Other_attributes, (originCamino1--costCamino1-->destinationCamino1), (originCamino2--costCamino2-->destinationCamino2), (..), (originCaminoN--costCaminoN-->destinationCaminoN)", N being the total number of Camino objects in the list. However, I keep getting the following error:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayStoreException
  at java.lang.System.arraycopy(Native Method)
  at java.util.ArrayList.toArray(Unknown Source)
  at apartado1.Posada.getStringToReturn(Posada.java:106)
  at apartado1.Posada.toString(Posada.java:121)
  at java.lang.String.valueOf(Unknown Source)
  at java.lang.StringBuilder.append(Unknown Source)
  </code></pre>
  
  <p>I know it's got to be something really easy to fix, but I've been looking around and it just won't work no matter what I do. I've tried using toString() and toArray(), but I get the same results. Here's hoping you can help me. Thanks in advance!</p>