我要创建2 ArrayList中。一个拿着16种颜色,另外一个拿着139。
I want to create 2 ArrayList. One holding 16 colors, the other one holding 139.
我有一个颜色列表(包括RGB为255,126,32和十六进制为0xFFFF2552)。我想使用的ArrayList以后随机挑选颜色。
I have the list with colors (both RGB as 255,126,32 and Hex as 0xFFFF2552). I want to use the ArrayList to later pick random colors from.
我已经试过INT [],这是行不通的。我试过的ArrayList<整数GT;
和的ArrayList<颜色>
。我的问题是;我不明白如何将颜色添加到的ArrayList
I've tried int[], that doesn't work. I've tried ArrayList<Integer>
and ArrayList<Color>
. My problem is; I don't understand how to add the colors to the ArrayLists.
谢谢!
现在,我在探索这样的:
For now, I'm exploring this:
Color cBlue = new Color(0,0,255);
Color cRed = new Color(255,0,0);
ArrayList colors = new ArrayList();
colors.add(cBlue);
colors.add(cRed);
等等...
and so on...
我真的很喜欢 INT []颜色= =新的INT [] {4,5};
,因为它只有一行code ...但如何我得到的色彩,到了后来,挑选?
I really like int[] colors = = new int[] {4,5};
because it's only one line of code... but how do I get colors in, to later on, pick from?
或..会是更好地存储在strings.xml中文件的颜色,然后从那里填写的ArrayList?如果是的话,我应该怎么办呢?
or.. would it be better to store the colors in a strings.xml file and then fill the ArrayList from there? If so, how should I do that?
谢谢!
您可以尝试:
int[] colors = new int[] {Color.rgb(1,1,1), Color.rgb(...)};
例如,但我不认为这是只用一条线的说法决定是个好主意。
For example, but I don't think it's a good idea to decide only using "one line" argument.
List<Integer> coloras = Arrays.asList(new Integer[]{Color.rgb(1, 1, 1), Color.rgb(...)});
也将工作。
您可以创建 arrays.xml
文件一个ArrayList
You can create an arraylist in arrays.xml
file:
<resources>
<string-array name="colors">
<item>#ff0000</item>
<item>#00ff00</item>
<item>#0000ff</item>
</string-array>
</resources>
然后使用循环读取它们:
Then use the loop to read them:
String[] colorsTxt = getApplicationContext().getResources().getStringArray(R.array.colors);
List<Integer> colors = new ArrayList<Integer>();
for (int i = 0; i < colorsTxt.length; i++) {
int newColor = Color.parseColor(colorsTxt[i]);
colors.add(newColor);
}
在我保持颜色列表中的意见是最方便易的解决方案。
In my opinion keeping colors in the list is the most convinient solution.
要采取从列表中选择颜色随机,你做的:
To take a color from the list randomly, you do:
int rand = new Random().nextInt(colors.size());
Integer color = colors.get(rand);
上一篇:Spring Boot + Springbox 招摇错误招摇、错误、Spring、Boot
下一篇:使用 ASP.NET Core Web API 重命名 Swashbuckle 6 (Swagger) 中的模型重命名、模型、Core、ASP