获得类的arraylist的索引.java的

use*_*196 0 java arrays class arraylist

我有班级名单A:

public class lista {
            int i;
            String name;

            public lista(int i, String name)
            {
                this.i = i;
                this.name = name;
            }
    }
Run Code Online (Sandbox Code Playgroud)

我从这个类中创建了ArrayList.

 public static ArrayList<lista> friends;
Run Code Online (Sandbox Code Playgroud)

添加一些日期:14 - 亚当2 - 约翰35 - 阿诺德74 - x 54 - x

我想获得IndexOf 74并将名称从x更改为Catrina.

怎么做?

friends.get(friends.indexOf(??)) = "catarina";
Run Code Online (Sandbox Code Playgroud)

cow*_*wls 5

看起来你会更好地使用a,Map因为它们能够更好地处理键值对,这就是你在这里所拥有的.

Map<Integer, String> friends = new HashMap<Integer, String>();

friends.put(14, "Adam");

friends.get(14); //Adam

friends.put(14, "John");

friends.get(14); //Now John
Run Code Online (Sandbox Code Playgroud)