Shadowph 0 Denunciar post Postado Junho 9, 2015 Eu tenho 2 ArrayList com o mesmo tipo de objeto. Preciso comparar os objetos dos 2, caso o primeiro tenha um objeto que n tenha no segundo, eu adiciono esse objeto no segundo ArrayList, ficando no final da iteração, os 2 arrays iguais. Como posso fazer isso? Compartilhar este post Link para o post Compartilhar em outros sites
reebr 94 Denunciar post Postado Junho 12, 2015 O que tentou? Compartilhar este post Link para o post Compartilhar em outros sites
Blackthog 1 Denunciar post Postado Junho 13, 2015 for (Object obj : first) { //Quando first = primeira arraylist if (array2.contains(obj)) { //Quando array2 = segunda arraylist //contém na primeira e na segunda array. continue; } else { //existe na 'first' (primeira array) mas não existe na 'array2' (segunda array). array2.add(obj); //adicionando na segunda array ('array2') continue; } } Nota: Lembre-se que você pode usar ao invés de 'Object' a própria classe na array, Compartilhar este post Link para o post Compartilhar em outros sites
Vergil 15 Denunciar post Postado Junho 16, 2015 Se isso não for um exercício da faculdade, tu poderias utilizar o método CollectionUtils.union(arrayList1, arrayList2). Ver: https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/CollectionUtils.html#union(java.util.Collection, java.util.Collection) Returns a Collection containing the intersection of the given Collections.The cardinality of each element in the returned Collection will be equal to the minimum of the cardinality of that element in the two given Collections. Compartilhar este post Link para o post Compartilhar em outros sites