Is there a more efficient way to set up a large ArrayList than adding one item at a time?

Everything I've looked at shows ArrayLists being built one item at a time, usually looping through. Isn't there some way you can just go ahead and DEFINE one all at once? I'm trying to make a 2D array of parameters for vector graphic PShapes, and they all have different numbers of parameters so it has to be flexible. There is no way to predict how much room will be needed for a given shape.

Tagged:

Answers

  • I mean, I do have an initial size and list, but the whole point of it is that these should be modifiable.

  • edited October 2016 Answer ✓

    @rickhatfield -- Could you provide brief example code? I'm not sure that I understand your use case here.

    It sounds like you want a syntax shortcut for initialization on declaration? This is possible, but (for reasons that have to do with the intricacies of object types in Java) initializing an ArrayList is not a simple or an intuitive feature of the language. Here is a concise example:

    /**
     * ArrayList Initialization
     * 2016-10-06 Jeremy Douglass
     * https:// forum.processing.org/two/discussion/18439/is-there-a-more-efficient-way-to-set-up-a-large-arraylist-than-adding-one-item-at-a-time#latest
     * For discussion see http:// stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line/3676539#3676539
     **/
    
    ArrayList<String> strings = new ArrayList<String>(java.util.Arrays.asList(
      "Lorem", "ipsum", "dolor", "sit", "amet", "consectetur",
      "adipiscing", "elit", "sed", "do", "eiusmod", "tempor",
      "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua."
    ));
    
    void setup(){
      size(200,200);
      for(String s : strings){
        print(s+' ');
      }
    }
    void draw(){
      background(0);
      text("ArrayList Initialization",10,20);
      text(strings.get(second()%strings.size()),10,height/2);
    }
    
  • Jeremy, that is exactly what I wanted to know -- how to initialize a large block of data for my arraylist. My hat is off to you, kind sir, and I thank you most sincerely for your helpfulness. But how on earth would anyone ever know that from the information presented in the Processing reference? That looks like quite an advanced and highly specific bit of java-jive.
    I am accustomed to languages that are not so rigidly data-typed as to force these sorts of contortions. Sigh.

  • edited October 2016

    @rickhatfield I think that, precisely because this is such specific jive, one would not recommend this kind of ArrayList initialization to beginners -- and so it would never be in the Processing documentation. See the stackoverflow link in my code example for some reasons why even Java experienced programmers don't necessarily recommend this approach to each other (although not all of their reasoning translates to Processing).

    If you have a large block of data for *List initialization and want it to be easy to hand edit / easy to read / don't want to write a long line of .add statements for some other reason, one approach would be to keep the data in a separate CSV or JSON file and loop over that with LoadTable or etc. Another approach would be to define your initialization data in a static array using the concise [] syntax and then loop through that to .add each.

    Re: data types, I know what you mean. Have you tried Python Mode? While more limited and with some significant known bugs, it is a very relaxed idiom to write Processing in.

  • No, I haven't heard of Python Mode, thanks, I'll look into that.
    Yes, I suppose I will initially initialize as you say. I suppose, once I have some files saved, I'll just be loading them one at a time anyway, which works well with this kind of data structure, so I guess it's starting to make a bit more sense to me, haha. Well, whats obvious to others is often far less so to me, and vice versa. And I am new to this kind of language. But not new to programming; I started out using Forth and Assembler way back when... I just haven't kept up with the times. Not web-savvy, etc. Ah well. Who has time for everything?

  • edited October 2016

    re:Python mode

    You would be programming in a different language (Python, not Java), and it can be confusing to switch back and forth, but here is info on Python mode, which you an install through the PDE app > Tools > Add Tool > Modes > Python Mode.

    There is less focus on requiring types and objects in Python, and the syntax is a bit more like shell script / bash -- whitespace, fewer brackets. You might like it.

    One caveat -- because it is a port of the Java version of processing, Python mode is a slightly smaller implementation (see the smaller reference list) and is less documented / supported / maintained. I think it works well, but your mileage may vary.

  • Thanks, Jeremy. I can't say I'm particularly taken with what I've seen of Python, but it's worth a try. "Mileage may vary", hahaha, indeed...

Sign In or Register to comment.