简体   繁体   English

将元素添加到数组列表/Pojo

[英]Adding elements to a array list/Pojo

I am having an issue with adding elements to an ArrayList.我在向 ArrayList 添加元素时遇到问题。 I created a POJO like my array list.我创建了一个 POJO,就像我的数组列表一样。

public class GroupUserMapping {
    String group;
    ArrayList<String> users;
//setters getters constructor
}

I have a function where I am reading a text file to get Groups which I stored in groups array now for those groups I need to loop through all the records in other text file and get all the users who have this group mapping and according to my condition I am trying to add elements to the Pojo ArrayList like this:我有一个 function ,我正在读取一个文本文件以获取我现在存储在组数组中的组,这些组我需要遍历其他文本文件中的所有记录并获取所有具有此组映射的用户,并根据我的条件我正在尝试向 Pojo ArrayList 添加元素,如下所示:

 ArrayList<GroupUserMapping> usergroups = new ArrayList< GroupUserMapping >();
        ArrayList<String> users = new ArrayList<>();
        ArrayList<String> groups = getGroups();// This used for lopping I have certain groups.
        Scanner s = null;

        String group="";
       for(int i= 0;i<groups.size();i++ ){
           s = new Scanner(new File(getClass().getClassLoader().getResource(mappingFile).getFile()));
           s.useDelimiter("\r\n");
           group=groups.get(i);
           users.clear();
           while (s.hasNext()) {
               String aLine = s.next();
               String[] value = aLine.split(":");

               if(group.equalsIgnoreCase(value[0]))
               {
                   LOGGER.info(group+":"+value[1]);
                   users.add(value[1]);
               }
           }
            GroupUserMapping groupUserMapping = new GroupUserMapping(group,users);
          
            usergroups.add(groupUserMapping);
           s.close();

The issue is when I am reading for Group say G1 I got users u1,u2 and these mapping is correctly stored in usergroups but next loop when I am searching for Group G2 I have users U2,U3.问题是当我为组说 G1 阅读时,我得到了用户 u1,u2,并且这些映射正确存储在用户组中,但是当我搜索组 G2 时,下一个循环我有用户 U2,U3。 Here in the 2nd loop, it is also updating 1st Group values as well.在第二个循环中,它也在更新第一组值。 Ideally my elements should be G1->U1,U2 and G2-> U2,U3 but my code is adding elements like this.理想情况下,我的元素应该是 G1->U1,U2 和 G2-> U2,U3 但我的代码正在添加这样的元素。 G1->U2,U3 G2-> U2,U3. G1->U2,U3 G2->U2,U3。 What I am doing wrong here?我在这里做错了什么?

The line users.clear();users.clear(); is causing the issue.导致问题。

In the first iteration you add users 1 and 2 to that list.在第一次迭代中,您将用户 1 和 2 添加到该列表中。 Then in the constructor you are passing this list.然后在构造函数中传递这个列表。

GroupUserMapping groupUserMapping = new GroupUserMapping(group,users);

Now in the second iteration, you clear the loop and then add users 2 and 3. Since the first group has the reference to the same list, G1's user list is also cleared and user 2 and 3 are added.现在在第二次迭代中,您清除循环,然后添加用户 2 和 3。由于第一个组引用了同一个列表,因此 G1 的用户列表也被清除并添加了用户 2 和 3。

You need to replace the users.clear();您需要替换users.clear(); with

users = new ArrayList<String>();

In the above case, the data is unchanged, only the users variable now points to a different location.在上述情况下,数据没有改变,只有users变量现在指向不同的位置。 Hence there won't be any changes to the list of G1.因此,G1 的列表不会有任何变化。

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

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