合并两个数组列表

时间:2016-05-04 10:23:55

标签: java

我有两个ArrayLists

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

在第一个数组中,我有1-60之间的条目(有些数字丢失)。 在第二个数组中是那些缺少的数字。

我想打印所有条目,例如....

存在的内容如1 --- Y

那些缺失的东西会像12 --- N

那样出现

这是我的代码:

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

但我的输出如下:

  

1 --- Y

     

1 --- Y

     

2 --- Y

     

2 --- Y.   等等......

4 个答案:

答案 0 :(得分:3)

你的问题似乎有点令人困惑......但你可能想要这个:

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

输出:

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

等等......

ADD ON's:

请记得关闭作者:

writer.close();

在您使用FileWritter的方式中,内容将附加在文件的末尾,在构造函数中使用false

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

测试它!

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

答案 1 :(得分:2)

用以下代码替换您的代码:

BEGIN
DECLARE my_delimiter CHAR(1);
DECLARE split_comma text;
DECLARE done INT;
DECLARE occurance INT;
DECLARE i INT;
DECLARE id INT;
DECLARE sel_query VARCHAR(500);
DECLARE splitter_cur CURSOR FOR SELECT id,comma from tmp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

OPEN splitter_cur;
    splitter_loop:LOOP
        FETCH splitter_cur INTO id,split_comma;
        SET occurance = LENGTH(split_comma) - LENGTH(REPLACE(split_comma,',',''))+1;
        SET my_delimiter=',';

        IF done = 1 THEN
            LEAVE splitter_loop;
        END IF;

        IF occurance > 0 THEN               
            SET i = 1;              
            WHILE i <= occurance DO
                SET sel_query = "SELECT SUBSTRING_INDEX(comma,',',i) as abc from tmp1"; 
                SET @sel_query = sel_query;
                PREPARE sel_query FROM @sel_query;      
                EXECUTE sel_query;                  
                SET i = i + 1;
            END WHILE;
        END IF;
        SET occurance = 0;
    END LOOP;
CLOSE splitter_cur;
END;

答案 2 :(得分:1)

  1. 您没有使用data列表 - 为什么在您的问题中根本提到它?您可能认为每个数字都在其中一个列表中。正如您已经提到过这个列表,我会写一个使用它的代码。

  2. Nomen omen a.k.a.使用更有意义的名字。 datatemp不是很有说服力的名字。

  3. 始终将列表声明为List,请勿使用该规范。

  4. 您说“数字1-60”,但您的for循环有上限< 60

  5. 使用Java 7功能“try-with-resources”,这将确保您的编写器始终关闭。

  6. 现在代码片段:

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

答案 3 :(得分:0)

以下是基于您的描述的不同方法(请注意,该示例修改了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");
    }
}