java - java中用元素返回名称的方法?

标签 java arraylist

Java 初学者,我寻找通过用户提供的 id 使用函数“readPeople ()”返回 ArrayList“this.list”中关联的名称的方法。 这是我的代码:

public class PersonneC implements PersonneDB {
    this.list = new ArrayList();

    People marie = new People(1, "Bower", "Marie");
    list.add(marie.toString());

      public PeopleWithId readPeople(int idPeople) {
        PeopleWithId peoplename = null;
        for(int i = 0; i < list.size(); i++) {
            if(liste.get(i).equals(idPeople)) {
                result = list.get(i);
            }
        }
        return result.getPeopleName(); //ERROR INCOMPATIBLE TYPES
        }

Wanted result returned: Bower

最佳答案

您的方法返回一个字符串readPeople的返回类型为PeopleWithId

关于java - java中用元素返回名称的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47231256/

相关文章:

java - 在 Java Spring 中将 JSON 绑定(bind)到 Arraylist?

java - Netbeans - 错误 : Could not find or load main class

Android ArrayList 迭代器返回 null

java - 填充数组列表的随机索引出现 NullPointerException?

java - 如何在 firebase 中获取直接父 key

java - 从 hashmap 存储和检索 ArrayList 值

java - 打印所有 JVM 标志

java - ProGuard 使应用程序无法使用 "could not convert to dex bytecode with error 1"进行编译

java - 如何将对象列表存储到内存中以供以后使用

java - Python调用Java导致错误: Could not find or load main class