简体   繁体   中英

Get certain value from arrayList

EDIT:

I attempted to create something that could do what I wanted it to do, but I got an error

String namn = JOptionPane.showInputDialog("Write persons name");
String dele = JOptionPane.showInputDialog("Yes or no? (Ja/nej)");
int pos = personLista.indexOf(namn);
personLista.get(pos).setDelegat(dele);
System.out.println("Done!");

Error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at finnish.jovo.main(jovo.java:29)

Doest that mean it couldn't find the name in the list?


I am trying to think of how to properly make a method that fetches a certain arrayList item depending on my input string.

So if I'm fe trying to get said value by writing the name in an input box.

Object:

protected String namn, enamn, kårtill, mötdel;
public Person(String namn, String enamn, String kårtill, String mötdel) {
    this.namn=namn;
    this.enamn=enamn;
    this.kårtill=kårtill;
}
public String getNamn(){
    return namn;
}
public String getKår() {
    return kårtill;
}
public void setNamn(String namn){
    this.namn=namn;
}
public void setDelegat(String mötdel) {
    this.mötdel=mötdel;
} 

List:

static List<Person> personLista =new ArrayList();

Essentially what I am trying to accomplish in somewhat gibberish code:

String name = JOptionPane.showInputDialog("Write persons name");
if (name exists in list) {
get position of name
String truefalse = JOptionPane.showInputDialog("True or false?");
setDelegat(truefalse) in said item
}

Would appreciate some help!

使用釉面列表并应用过滤器。使用釉面列表过滤器可以达到此目的。 查看http://www.glazedlists.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM