简体   繁体   English

合并两个数组列表

[英]Merging Two ArraysLists

I have Two ArrayLists 我有两个ArrayLists

  1. ArrayList<Integer> data = new ArrayList<Integer>();
  2. ArrayList<Integer> temp = new ArrayList<Integer>();

In the first array i have entries between 1-60(Some Numbers are Missing). 在第一个数组中,我有1-60之间的条目(缺少一些数字)。 In the second array are those missing numbers. 在第二个数组中是那些缺失的数字。

I want to print all entries Such as.... 我要打印所有条目,例如...。

Which are present would apperar like 1 --- Y 存在的东西会像1 --- Y一样

And those which are missing would appers like 12 --- N 而那些缺少的人会像12 --- N

Here is my code: 这是我的代码:

writer=new PrintWriter(new FileWriter(file,true));
                    for(int i=1;i<60;i++){
                        for(int missing:temp){
                        if(missing==i){
                            writer.println(i+"   +++++++++++++++++   N");
                        }
                        else{
                            writer.println(i+"   ---   Y");
                        }
                    }
                }

but my output is like: 但我的输出是这样的:

1 --- Y 1 ---

1 --- Y 1 ---

2 --- Y 2 ---

2 --- Y and so on..... 2 --- Y等.....

You question seems a bit confusing... but you may want this: 您的问题似乎有点令人困惑...但是您可能想要这样:

for (int actual = 1; actual <= 60; actual ++) {
    // number present in DATA list
    if (data.contains(actual)) {
        writer.println(actual + "   ---   Y");
    // number present in TEMP list
    } else if (temp.contains(actual)) {
        writer.println(actual + "   ---   N");
    // number not present in ANY list
    } else {
        writer.println(actual + "   -------");
    }
}

OUTPUT: 输出:

1   ---   Y
2   ---   Y
3   ---   N
4   ---   Y
5   ---   Y
6   ---   Y
7   ---   N
8   ---   N
9   -------
10   -------
11   -------

And so on... 等等...


ADD ON's: 添加的:

Remember to close the writer: 记住关闭作者:

writer.close();

In the way you use FileWritter , content will be appended at the end of the file, use false in the constructor: 使用FileWritter的方式,内容将附加在文件末尾,在构造函数中使用false

PrintWriter writer = new PrintWriter(new FileWriter(file, false));

TEST IT! 测试吧!

public class Q37024973 {
    public static void main(String[] args) throws Exception {
        ArrayList<Integer> data = new ArrayList<Integer>();
        data.add(1);
        data.add(2);
        data.add(4);
        data.add(5);
        data.add(6);
        ArrayList<Integer> temp = new ArrayList<Integer>();
        temp.add(3);
        temp.add(7);
        temp.add(8);

        File file = new File("C:\\tmp\\Q37024973.txt");

        PrintWriter writer = new PrintWriter(new FileWriter(file, true));

        for (int actual = 1; actual <= 60; actual ++) {
            if (data.contains(actual)) {
                writer.println(actual + "   ---   Y");
            } else if (temp.contains(actual)) {
                writer.println(actual + "   ---   N");
            } else {
                writer.println(actual + "   -------");
            }
        }

        writer.close();
    }
}

Replace Your Code With: 将您的代码替换为:

writer=new PrintWriter(new FileWriter(file,true));
    for(int i=1;i<=60;i++){
        if(temp.contains(i)){
            writer.println(i+"   +++++++++++++++++   N");
        }
        else{
            writer.println(i+"   ---   Y");
    }
}
  1. You are not using your data list - why are you mentioning it at all in your question? 您没有使用data列表-为什么在问题中完全提到它? You probably suppose that each number is exactly in one of the lists. 您可能假设每个数字都完全在列表之一中。 As you already mentioned this list, I will write you a code which uses it. 正如您已经提到的该列表,我将为您编写一个使用它的代码。

  2. Nomen omen aka use better meaningful names. Nomen预兆也使用更好的有意义的名称。 data and temp are not very eloquent names. datatemp名称不是很雄辩。

  3. Always declare lists as List , do not use the specification. 始终将List声明为List ,不要使用规范。

  4. You say "numbers 1-60", but your for-loop has upper limit < 60 . 您说“数字1-60”,但您的for循环上限为< 60

  5. Use Java 7 feature "try-with-resources", this will ensure that your writer is consistently closed. 使用Java 7功能“ try-with-resources”,这将确保您的编写器始终处于关闭状态。

Now the code snippet: 现在的代码片段:

List<Integer> existingNumbers = new ArrayList<>();
List<Integer> missingNumbers = new ArrayList<>();

try (Writer writer = new PrintWriter(new FileWriter(file,true))) {
    for (int i=1; i<=60; i++) {
        if (missingNumbers.contains(i)) {
            writer.println(i+"   ---   N");
        }
        if (existingNumbers.contains(i)) {
            writer.println(i+"   ---   Y");
        }
    }
}

Here is a different approach based on your description (note that the example modifies the data object): 这是根据您的描述的另一种方法(请注意,该示例修改了data对象):

List<Integer> data = new ArrayList();
data.add(1);
data.add(2);
data.add(4);

List<Integer> temp = new ArrayList();
temp.add(3);
temp.add(5);

data.addAll(temp);
Collections.sort(data); //do you want sorting?

for(Integer i : data){
    if(temp.contains(i)){
        System.out.println(i+" --- Y");
    }
    else{
        System.out.println(i+" ---- N");
    }
}

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

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