繁体   English   中英

仅打印第一列中具有重复值的2D数组的第一个元素

[英]Only printing the first element of a 2D array with repeating values in first column

我有一个二维数组,看起来像这样。

10002,20
10004,72
10008,12
10010,37
10010,34
10007,28
20003,42
20003,38
10002,16

如您所见,它在第一列中有一些重复的数字,例如10010和20003,我只想打印弹出的每个中的第一个。 所以这意味着我希望println打印出来

10002,20
10004,72
10008,12
10010,37
10007,28
20003,42
结束

(要注意的是:第一列中出现重复数字的第一个元素在第二列中始终具有最大的整数,例如:始终为10010,37> 34和20003,42> 38。)但我不确定怎么做...

编辑:这是带有XLS文件摘要的完整代码

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Read {

            public static void readXLSFile() throws IOException{
        InputStream ExcelFile = new FileInputStream("C:/Sales Data.xls");
        HSSFWorkbook wb = new HSSFWorkbook(ExcelFile);
                HSSFSheet sheet=wb.getSheetAt(0);

                int numRows = sheet.getPhysicalNumberOfRows(); 
                int[][] idSale = new int[numRows][2];

                for(int i=1;i<numRows;i++){
                    HSSFCell proId = sheet.getRow(i).getCell(1);
                    HSSFCell sales = sheet.getRow(i).getCell(2);
                    idSale[i][0]=(int)proId.getNumericCellValue();
                    idSale[i][1]=(int)sales.getNumericCellValue();
                }
                for(int j=1;j<numRows;j++){
                    for(int jj=j+1;jj<numRows;jj++)
                        if(idSale[j][0]==idSale[jj][0]){
                           idSale[j][1]+=idSale[jj][1];
//the problem with this loop is that there are repeated numbers in 
//the first column as I'm comparing the entire array to a copy of itself
//and I'm not sure how to avoid it...
                        }
                }
            }



            public static void main(String[] args) throws IOException {
        readXLSFile();
            }
}

这是我正在读取的XLS文件的摘要。 http://postimg.org/image/drbq7fucz/此程序旨在灵活使用,并且可用于具有相同格式的excel文件。 基本上,分配是添加匹配产品ID的单位,然后以行/列格式将它们吐回去。 客户ID无关紧要。 我无法预设数组大小,因为该程序必须能够读取可能具有不同行数的不同excel文件...

存储已打印的值的列表/集。

//provided int[][] rows;
Set<Integer> printed = new HashSet<>();
for(int[] row: rows){
    int before = printed.size();
    printed.add(row[0]);
    if(printed.size()>before){
        System.out.println(Arrays.toString(row));
    }
}

这遍历了数组中的所有行。 打印的集包含已打印的行的所有第一个值。 将before设置为在添加新元素之前的打印尺寸。

printed.add(row[0]);printed.add(row[0]); 被称为发生以下两种情况之一:值已经在打印文件中,因此打印的大小不会改变。 该值未打印,因此添加了元素。

仅在之前未打印过元素的情况下,“ printed.size()>before ”检查printed.size()>before为真。

这是做到这一点的一种方法。 Set不能包含重复元素。

Set<Integer> printed = new HashSet<>();

for (int[] item : list) {
   if(!printed.contains(item[0]) {
      printed.add(item[0]);
      // Print your line
   }
}

以下示例程序将完成此工作:

public static void main(String[] args) {
        int[][] sample = {{10002, 20},
                {10004, 72},
                {10008,12},
                {10010,37},
                {10010,34},
                {10007,28},
                {20003,42},
                {20003,38}
        };

        Set<Integer> unique = new HashSet<>();
        boolean newAddition = false;
        for(int[] row : sample) {
             newAddition = unique.add(row[0]);
             if(newAddition) {
                 System.out.println(Arrays.toString(row));
             }
        }
    }

输出为:

[10002, 20] [10004, 72] [10008, 12] [10010, 37] [10007, 28] [20003, 42]

说明:它使用Set ,不允许重复。 当我们向集合中添加元素时,它将返回布尔值,如果最近添加的元素是唯一的,则返回true。 在这种情况下,我们将打印数组中的特定行。

假设值如示例中所示进行分组,这是最简单的方法:

int[][] array = { {10002,20},
                  {10004,72},
                  {10008,12},
                  {10010,37},
                  {10010,34},
                  {10007,28},
                  {20003,42},
                  {20003,38} };
int prev = array[0][0] - 1; // make sure it's different from first value
for (int[] pair : array) {
    if (pair[0] != prev) {
        System.out.printf("%d,%d%n", pair[0], pair[1]);
        prev = pair[0];
    }
}

输出量

10002,20
10004,72
10008,12
10010,37
10007,28
20003,42

暂无
暂无

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

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