How to put a StringList in a constructor?

I have this, and I think its all wrong, I can't get my head around the syntax Also, how must I put data into this class?

class StoreStuff 
{
private ArrayList<String> botle   ;
private ArrayList<String> apple ; 

public StoreTReesStuff ( String botle, String apple )
{
this.botle   = new ArrayList<String>();
this.apple = new ArrayList<String>();

}
void addApple()
{
apple.append("Jonagold");
}

}// end

void draw()
{
InstStoreStuff.addApple();
}

Answers

  • Answer ✓

    I assume that line 6 is the start of the constructor, in which case it should be named after the class. And if you want to pass an ArrayList to the constructor you get -

    class StoreStuff {
        private ArrayList<String> botle   ;
        private ArrayList<String> apple ;
    
        public StoreStuff ( ArrayList<String> botle, ArrayList<String> apple ) {
            this.botle   = botle;
            this.apple = apple;
        }
    
        void addApple() {
            apple.add("Jonagold");
        }
    
    }  // end
    
  • edited October 2014 Answer ✓

    My take would be something like this: =:)

    // forum.processing.org/two/discussion/7657/
    // how-to-put-a-stringlist-in-a-constructor
    
    class StoreStuff {
      final StringList bottles = new StringList();
      final StringList apples  = new StringList();
    
      StoreStuff addBottle(String... bottle) {
        bottles.append(bottle);
        return this;
      }
    
      StoreStuff addApple(String... apple) {
        apples.append(apple);
        return this;
      }
    
      @ Override String toString() {
        return "StoreStuff:\n"
          + bottles.toString() + "\n"
          + apples.toString();
      }
    }
    
    final StoreStuff stuffs = new StoreStuff();
    
    void setup() {
      stuffs
        .addBottle("Katniss", "Magnetic_Garden")
        .addApple("Jonagold");
    
      println(stuffs);
      exit();
    }
    
  • Thank you for both answers, you guys have cleared up my confusion, now I can finish my project at last :) The last answer seems to be the most versatile, so I go with that.

    Sorry about the typo, in the constructor, I remade my problem into something that I could post, and forgot to change something.

Sign In or Register to comment.