简体   繁体   English

将元素存储到ArrayList中却得到了冗余?

[英]Storing elements into an ArrayList but getting redundancies?

I edited the question: I'm trying to store foreign keys of all tables of a database in an arraylist Fkeys. 我编辑了一个问题:我试图将数据库的所有表的外键存储在arraylist Fkeys中。 I have the method getFK that retrieves foreign keys (if they exist) of existing tables 我有方法getFK检索现有表的外键(如果存在)

public ArrayList Fkeys = new ArrayList();
public ArrayList Vec = new ArrayList();

public ArrayList getFK () {
    try {

        /* Database connection */

       DBConnection connect = new DBConnection();
       connect.DBConnect();
       Connection con = connect.con;
       /* Getting some infos regarding keys*/
       DatabaseMetaData metadata = con.getMetaData();
       ResultSet clefs = metadata.getImportedKeys(null, null, "persons");

while(clefs.next()) {
    Vec.add(clefs.getString("FKCOLUMN_NAME"));
                   }
    Fkeys.add(Vec);


    clefs = metadata.getImportedKeys(null, null, "departments");

while(clefs.next()) {
    Vec.add(clefs.getString("FKCOLUMN_NAME"));
                   }
    Fkeys.add(Vec);
}

Now when I execute this I get this result : [[ ], [ ]] because the two tables persons and departments have no foreign key. 现在,当我执行此操作时,将得到以下结果:[[],[]],因为两个表的人员和部门没有外键。 But when I add these lines to the method regarding the table students which has 3 foreign keys 但是当我将这些行添加到有关具有3个外键的表格学生的方法中时

clefs = metadata.getImportedKeys(null, null, "students");

while(clefs.next()) {
    Vec.add(clefs.getString("FKCOLUMN_NAME"));
                   }
    Fkeys.add(Vec);

I get this result : [[id, person, depp], [id, person, depp], [id, person, depp]] While it is supposed to be: [[id, person, depp], [ ], [ ]] What I'm doing wrong? 我得到这个结果:[[id,person,depp],[id,person,depp],[id,person,depp]]应该是:[[id,person,depp],[],[ ]]我在做什么错? and why the content of Vec is stored in all positions of Fkeys? 为什么Vec的内容存储在Fkeys的所有位置?

This loop seems to be adding the same thing to alist each time ... 这个循环似乎每次都会在alist添加相同的内容...

    while(keys.next()) {
          alist.add(clefs.getString("FKCOLUMN_NAME"));  
    }

I can't be sure this this is the problem ('cos you don't tell us what clefs is!) ... but it looks mighty suspicious to me. 我不能确定这是问题所在('cos,您不告诉我们clefs是什么!)...但是对我来说,它似乎可疑。

Check out Sets , they're like Lists, but they have the ability to be unique inside the object. 检出Sets ,它们就像Lists一样,但是它们具有在对象内部唯一的能力。 Ie if you have: 即如果您有:

Set<Integer> intSet = new HashSet<Integer>()
intSet.add(1);
intSet.add(1);
for (Integer i : intSet) {
     System.out.println(i);
}

The final output should be: 最终输出应为:

1

Sets don't allow multiple Identical Objects in the same set. 集合不允许同一集合中有多个相同的对象。

Also, When you were declaring your ArrayLists, be sure to note what kind of an ArrayList each one is, otherwise your IDE should be yelling at you, but I think it defaults to Object. 另外,在声明ArrayList时,一定要注意每个ArrayList的类型,否则您的IDE应该会大吼大叫,但我认为它默认为Object。 I recommend having a custom model object if you need 3 fields in the Set/List and declare it like this List<CustomModelObject> listOfObjects = new ArrayList<CustomModelObject>(); 如果在Set / List中需要3个字段并像这样进行声明,则我建议使用一个自定义模型对象List<CustomModelObject> listOfObjects = new ArrayList<CustomModelObject>(); (see above example if you need an example for Set) (如果需要Set的示例,请参见上面的示例)

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

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