简体   繁体   English

获取类的数组列表的索引。 爪哇

[英]Getting indexof arraylist of class. java

I have class listA: 我有班级名单A:

public class lista {
            int i;
            String name;

            public lista(int i, String name)
            {
                this.i = i;
                this.name = name;
            }
    }

I made ArrayList from this class. 我从此类制作了ArrayList。

 public static ArrayList<lista> friends;

Adding some date: 14 - Adam 2 - John 35 - Arnold 74 - x 54 - x 添加一些日期:14-亚当2-约翰35-阿诺德74-x 54-x

and i want to get IndexOf 74 and change name from x to Catrina. 我想获得IndexOf 74并将名称从x更改为Catrina。

How to do it? 怎么做?

friends.get(friends.indexOf(??)) = "catarina";

It looks like you would be better of using a Map as they are much better equipped to handle key value pairs which is what you have here. 看起来您最好使用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

This is not how it works. 这不是它的工作方式。 The i in your class lista is not the index of where it is in the list. listai并不是lista位置的索引。

First of all, you have to change your class. 首先,您必须更改班级。 This is not nice coding. 这不是很好的编码。

public class Friend { 
       //note1: Class Names Start With Capital Letter!
       //note2: Class names have meaning!

        private int i; //notice private modifier. 
        private String name; //notice private modifier.

        public Friend (int i, String name)
        {
            this.i = i;
            this.name = name;
        }
        /* getter and setter methods */
        public int getI() {
            return i;
        }
        public String getName()
            return name;
        }
        public void setName(String name)
            this.name = name;
        }
      /* don't forget equals() and hashCode() */

}

Of course, a proper equals() and hashCode() method is crucial to be able to workwith them properly - but there is an abundance of materials regarding this subject on the net... 当然,适当的equals()和hashCode()方法对于正确使用它们至关重要-但网络上有大量有关此主题的资料...

Given that class, you have to traverse the list to find the elements: 给定该类,您必须遍历列表以查找元素:

for(Friend friend: friends) {
    if(&& friend.getI()==74) {
        friend.setName("Cristina");
    }
}

But from here, there are still some improvements to make. 但是从这里开始,仍然需要进行一些改进。 The approach of cowls is in the right direction, but I'd take it one step further, by using a Map<Integer, Friend> : 整流罩的方法是朝正确的方向发展,但我将使用Map<Integer, Friend>进一步向前迈进:

//create Map
Map<Intger, Friend> friends = new HashMap<Integer, Friend>();
//note: use interface for declaring variables where possible to hide implementation specifics!

//add friends:
friends.put(75, new Friend(75,"a")); 
//... left ot others for brevity

//modify part:
Friend f = friends.get(75);
if(f!=null) { //always check for null
   f.setName("Christina");
}

What more do you get compared to cowls' approach? 与整流罩的方法相比,您还能得到什么? If you want to add fields to the Friend class - you are free to do that without pain of having to deal with converting everything... 如果您想将字段添加到Friend类中-您可以自由地执行此操作而不必担心转换所有内容...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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