Fill ArrayList with colors for Android

后端 未结 5 649
夕颜
夕颜 2021-01-21 01:52

I want to create 2 ArrayList. One holding 16 colors, the other one holding 139.

I have the list with colors (both RGB as 255,126,32 and Hex as 0xFFFF2552). I want to use

5条回答
  •  醉酒成梦
    2021-01-21 02:24

    What you have looks like it makes sense, but you should specify the type of the ArrayList.

    List colorList = new ArrayList();
    colorList.add(cBlue);
    ... etc
    

    (The difference between declaring it as a List or an ArrayList is that List is the interface that ArrayList implements. This is a generally pretty good practice, but for your purposes it probably won't make a difference if you declare it as a List or an ArrayList).

    If you want to do it in fewer lines of code, you can use an ArrayList initializer block, like this:

    List colorList = new ArrayList { new Color(...), cBlue };
    

    Or with Arrays.asList, which takes in an array and returns a List.

    But in general you should get used to verbosity with Java, don't try to optimize your lines of code so much as the performance of those lines.

    (Sidenote: make sure you're using the correct Color class. There's android.graphics.Color and java.awt.Color, and they're totally different, incompatible types. The constructor you're using is from java.awt.Color, and with Android you're probably going to want to use android.graphics.Color).

提交回复
热议问题