使用Arraylist处理Java代码setLabel错误

发布于 2025-01-17 18:19:05 字数 3167 浏览 3 评论 0 原文

我从一个文本文件创建了一个数组,然后在检测到某些内容后将其拆分为另外 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) 不适用于参数(对象)”

我不知道为什么会收到此错误,因为我拉的应该是一个字符串

I made an array from a text file then split it into 3 more arrays after it detects something. Then it would skip the first element then add whatever is after into 1/3 arrays. The problem is when I use .setLabel(btn1Array.get(0)) it says the function "setLabel()" expects parameters like: "setLabel(String)" but I thought the arraylist is already a string. This is a snip of my code

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))        

This is my textfile

------- 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

However I get this error

"The method setLabel(String) in the type Controller is not applicable for the arguments (Object)"

I dont know why I get this error since what I pulled should be a string

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2025-01-24 18:19:05

您需要明确地告诉处理您要存储的阵列列表的内容是字符串。否则,它假设它们是通用的“对象”。

三个潜在修复程序(选择一个):

  1. 让处理知道存储在阵列列表中的所有内容都是字符串。当您首次声明变量时,您可以这样做:
ArrayList<String> btn1Array = new ArrayList<String>();

您可以通过在 arrayList 之后将类型放在角度括号中来执行此操作。您可以在粒子键入此示例。 /a>

  1. 当您将其从阵列列表中拉出时,您可以将值施放为字符串。这告诉处理,即使假定它是一个对象,您也知道它应该是一个字符串:
.setLabel((String) btn1Array.get(0)) 

您通过将类型直接放在要施放的值之前将类型放在括号中。

  1. 您可以使用 stringlist 而不是arraylist。
    这更少是用于处理#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):

  1. Let Processing know that the type of everything stored in the ArrayList is a string. You do this when you first declare the variable:
ArrayList<String> btn1Array = new ArrayList<String>();

You do this by putting the type in angle brackets after ArrayList. You can see this with the Particle type in the example in the docs for ArrayList

  1. You can cast the value as a String when you pull it out of the ArrayList. This tells Processing that even though it assumes it's an Object, you know it should be a string:
.setLabel((String) btn1Array.get(0)) 

You do this by putting the type in parentheses directly before the value you want to cast.

  1. You can use StringList instead of ArrayList.
    This is more a less a shortcut built in to Processing for doing #1 (creating a list of Strings).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文