使用Arraylist处理Java代码setLabel错误
我从一个文本文件创建了一个数组,然后在检测到某些内容后将其拆分为另外 3 个数组。然后它会跳过第一个元素,然后将后面的元素添加到 1/3 数组中。问题是当我使用 .setLabel(btn1Array.get(0)) 时,它说函数“setLabel()”需要如下参数:“setLabel(String)”,但我认为 arraylist 已经是一个字符串。这是我的代码的片段
void setup() { // same as Ardunio program
String[] settings = loadStrings("settings.txt"); // Converts each line from the text file into strings of code
List btn1Array = new ArrayList();
List btn2Array = new ArrayList();
List btn3Array = new ArrayList();
for(int i = 0; i< settings.length; i++){ // Reads entire text file
String currentLine = settings[i];
if(currentLine.startsWith("btn")){ // Reads textfile, if line starts with "btn" does something
String stringArray[] = currentLine.split(" "); // Splits string into different elements if there is a space
String newArr[] = Arrays.copyOfRange(stringArray, 1, stringArray.length); // Skips first element and copys other elements into a new array
String value = String.join(" ", newArr); // Joins other elements into one string if there is a space between 2 elements (ie: Blue Lights)
if(currentLine.startsWith("btn-1")){ // If line starts with btn-1
btn1Array.add(value); // Add element into last place of btn1Array
} else if(currentLine.startsWith("btn-2")){ // If line starts with btn-2
btn2Array.add(value); // Add element into last place of btn2Array
} else if(currentLine.startsWith("btn-3")){ // If line starts with btn-3
btn3Array.add(value); // Add element into last place of btn3Array
}
}
}
size(700,900); // Window size, (width, height)
printArray(Serial.list()); // prints all avalible serial ports
port = new Serial(this, "COM13", 9600); // COM where arduino is connected
font1 = createFont("Arial Black",30,true); // Arial, 30 point, anti-aliasing on
font2 = createFont("Arial Black",23,true); // Arial, 23 point, anti-aliasing on
cp5 = new ControlP5(this);
Button1 = cp5.addToggle("button1") // Sets button1
.setLabel(btn1Array.get(0))
这是我的文本文件
------- NAME OF BUTTON 1 -------
btn-1-name: Lights
------- BACKGROUND COLOUR OF BUTTON 1 -------
btn-1-BGcolourR: 255
btn-1-BGcolourB: 165
btn-1-BGcolourG: 0
------- FOREGROUND COLOUR OF BUTTON 1 -------
btn-1-FGcolourR: 255
btn-1-FGcolourB: 100
btn-1-FGcolourG: 0
但是我收到此错误
“控制器类型中的方法 setLabel(String) 不适用于参数(对象)”
我不知道为什么会收到此错误,因为我拉的应该是一个字符串
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要明确地告诉处理您要存储的阵列列表的内容是字符串。否则,它假设它们是通用的“对象”。
三个潜在修复程序(选择一个):
您可以通过在
arrayList
之后将类型放在角度括号中来执行此操作。您可以在粒子键入此示例。 /a>您通过将类型直接放在要施放的值之前将类型放在括号中。
这更少是用于处理#1的处理(创建字符串列表)的快捷方式。
You need to explicitly tell Processing that the things you are storing the ArrayList are Strings. Otherwise it assumes they are generic "Objects".
Three potential fixes (choose one):
You do this by putting the type in angle brackets after
ArrayList
. You can see this with theParticle
type in the example in the docs for ArrayListYou do this by putting the type in parentheses directly before the value you want to cast.
This is more a less a shortcut built in to Processing for doing #1 (creating a list of Strings).