Solved Stringing ArrayList Together

Discussion in 'Plugin Development' started by Failplease, Jan 2, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Failplease

    How would I get all the contents of an ArrayList and add them to each other in a string?

    For example, the following code would add strings to an ArrayList:
    Code:
    ArrayList<String> str = new ArrayList<String>();
     
    str.add("i");
    str.add("like");
    str.add("pie");
    Now I want to make it so I can get the string "i like pie" from the ArrayList.
     
  2. Offline

    Sagacious_Zed Bukkit Docs

  3. Offline

    Failplease

    Sagacious_Zed
    Okay thanks, I know how to do most of this now.

    However, how would I do this:
    Code:
    List<String> str = new ArrayList<String>();
     
    str.add("i");
    str.add("like");
    str.add("pie");
     
    str.get(0);
    //all the up to:
    str.get(str.size());
    //but without putting the get methods in a for loop
     
  4. Offline

    Forseth11

    Failplease You would have to do some sort of loop. There is no way to change to code every time the size is changes. Why would you not want to use a loop?
     
  5. Offline

    Bart

    Code:java
    1. String msg = "";
    2. for(String s : list){
    3. msg+=s + " ";
    4. }
    ??
     
  6. Offline

    NathanWolf

  7. Offline

    Failplease

    Forseth11 and Bart
    If I use a loop, then I will not be able to access the message from outside of the loop.
     
  8. Offline

    McMhz

    Code:java
    1. for(int i = 0; i<str.length; /*Not sure if this is right, either length or size()*/ i++){
    2.  
    3. stuff = stuff + " " + str.get(i);
    4.  
    5. }
     
  9. Offline

    Bart

    Don't use a local variable then.
     
  10. Offline

    Failplease

    Bart
    I'm not that advanced...
    How do I not use a local variable?
     
Thread Status:
Not open for further replies.

Share This Page