【问题标题】:copying elements of an arraylist to another array list eliminating redundancy in the 2nd arraylist using Java使用Java将arraylist的元素复制到另一个arraylist消除第二个arraylist中的冗余
【发布时间】:2016-04-08 16:59:58
【问题描述】:

我想创建一个代码来帮助我将元素从“allWords”ArrayList 复制到“distinctWords”。但是他在distinctwords ArrayList中的时间我希望一个词只出现一个,消除“distinctWords'ArrayList中的冗余。

这是我想出的代码,我没有得到任何输出。

导入 java.util.ArrayList;

公共类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());

}

}

【问题讨论】:

    标签: java arraylist


    【解决方案1】:

    下面的代码怎么样:

    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);
        }
     }
    

    如果您不关心单词的顺序,那么您可以按照建议使用 Set。

    Set<String> distinctSet = new HashSet<String>(allWords);
    
    // if you want the set put into your other arrayList
    distinctWords.addAll(distinctSet);
    

    【讨论】:

    • 非常感谢迈克尔。那个 sn-p 工作得很好。
    【解决方案2】:
    //al1 and al2 are ArrayList<Object>
    for(Object obj:al1){
        if(!al2.contains(obj))
            al2.add(obj);
    }
    

    【讨论】:

    • 谢谢你。正是我需要的
    【解决方案3】:

    我认为这可以为您提供一组所有不同的单词。

    Set<String> distinctWords = new HashSet<>(allWords);
    

    如果你确实需要一个数组,你可以这样做:

    ArrayList<String> distinctWordsArr = new ArrayList<>(distinctWords);
    

    【讨论】:

    • 非常感谢您,我没有特别需要订购,我尝试了您的建议并且效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2018-07-21
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    相关资源
    最近更新 更多