[英]copying elements of an arraylist to another array list eliminating redundancy in the 2nd arraylist using Java
I would like to create a code that will help me copy elements from the 'allWords' ArrayList to the 'distinctWords'. 我想创建一个代码,以帮助我将元素从“ allWords” ArrayList复制到“ distinctWords”。 but his time in the distinct words ArrayList I want a word to appear just one, eliminating redundancy in the "distinctWords' ArrayList. 但是他在ArrayList中使用不同单词的时间我希望一个单词只出现一个,从而消除了“ distinctWords” ArrayList中的冗余。
this is the code that I have come up with and I am getting no output. 这是我想出的代码,但没有输出。
import java.util.ArrayList; 导入java.util.ArrayList;
public class copyElements { 公共类copyElements {
static ArrayList<String> distinctWords = new ArrayList<String> ();
static ArrayList<String> allWords = new ArrayList<String> ();
static int allWordsCount = 0;
static int distinctWordsCount;
static int tracker;
public static void main(String[] args) {
allWords.add("you");
allWords.add("want");
allWords.add("to");
allWords.add("go");
allWords.add("to");
allWords.add("dubai");
allWords.add("and");
allWords.add("you");
allWords.add("also");
allWords.add("want");
allWords.add("to");
allWords.add("go");
allWords.add("to");
allWords.add("seychelles");
//printing all items in the 'allwords' arraylist
//System.out.println("CONTENTS OF 'ALL WORDS' ARRAYLIST : ");
distinctWords.add(0,allWords.get(0));
distinctWordsCount = 1;
int i = 1;
for(int j = 1; j <= distinctWords.size(); j++){
if(i < allWords.size()){
tracker = 0;
if (tracker == j){
distinctWords.add(j, allWords.get(i));
System.out.println("\t\t\t" + distinctWords.get(j));
distinctWordsCount ++;
i++;
} else
if (tracker != j){
if(allWords.get(i) != distinctWords.get(tracker)){
//distinctWords.add(allWords.get(i));
//distinctWordsCount ++;
tracker++;
}
else
if(allWords.get(i) == distinctWords.get(tracker)){
i++;
}
//System.out.println("CONTENTS OF 'ALL WORDS' ARRAYLIST : ");
//System.out.println("\t\t\t" + distinctWords.get(j));
}
}
//System.out.println("\t\t\t" + distinctWords.get(j));
}
//System.out.println("\n\nTHE NUMBER OF ITEMS IN THE 'ALLWORDS' ARRAYLIST IS : " + allWords.size());
//System.out.println("\n\nTHE NUMBER OF ITEMS IN THE 'DISTINCTWORDS' ARRAYLIST IS : " + allWords.size());
System.out.println("\n\nITEMS IN THE 'DISTINCTWORDS' ARRAYLIST ARE : " + distinctWords.size());
}
} }
//al1 and al2 are ArrayList<Object>
for(Object obj:al1){
if(!al2.contains(obj))
al2.add(obj);
}
How about the following code: 下面的代码如何:
for (int i = 0, size = allWords.size(); i < size; i++)
{
String word = allWords.get(i);
// add the word if it is not in distinctWords
if (!distinctWords.contains(word))
{
distinctWords.add(word);
}
}
If you don't care about the order of the words, then you could use a Set as suggested. 如果您不关心单词的顺序,则可以按照建议使用Set。
Set<String> distinctSet = new HashSet<String>(allWords);
// if you want the set put into your other arrayList
distinctWords.addAll(distinctSet);
I think this can give you a set of all distinct words. 我认为这可以为您提供所有不同的词。
Set<String> distinctWords = new HashSet<>(allWords);
If you do need an array, you can simple do this: 如果确实需要数组,则可以简单地执行以下操作:
ArrayList<String> distinctWordsArr = new ArrayList<>(distinctWords);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.