简体   繁体   English

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

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

I have a 2D array that looks like this. 我有一个二维数组,看起来像这样。

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

As you can see, it has some repeating numbers in the first column such as 10010 and 20003, and I only want to print out the first one of each that pops up. 如您所见,它在第一列中有一些重复的数字,例如10010和20003,我只想打印弹出的每个中的第一个。 So that means I want the println to print out 所以这意味着我希望println打印出来

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

(KEY NOTE:The first element that pops up with repeating numbers in the first column will always have the largest int in the second column, EX: 10010,37>34 and 20003,42>38 ALWAYS.) but I'm not sure how to do so... (要注意的是:第一列中出现重复数字的第一个元素在第二列中始终具有最大的整数,例如:始终为10010,37> 34和20003,42> 38。)但我不确定怎么做...

Edit: Here is the full code with a snippet of the XLS file 编辑:这是带有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();
            }
}

This is a snippet of the XLS file I'm reading off of. 这是我正在读取的XLS文件的摘要。 http://postimg.org/image/drbq7fucz/ This program is meant to be flexible and be used on excel files with the same format. http://postimg.org/image/drbq7fucz/此程序旨在灵活使用,并且可用于具有相同格式的excel文件。 Basically the assignment is to add the units of matching product ID's and then spit them back out afterwards in a row/column format. 基本上,分配是添加匹配产品ID的单位,然后以行/列格式将它们吐回去。 Customer ID is irrelevant. 客户ID无关紧要。 I can't preset array size because the program must be able to read different excel files that may have different numbers of rows... 我无法预设数组大小,因为该程序必须能够读取可能具有不同行数的不同excel文件...

Store a List/Set of values that you have printed. 存储已打印的值的列表/集。

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

This goes through all of the rows in the array. 这遍历了数组中的所有行。 The Set printed contains all of the first value of the rows that have been printed. 打印的集包含已打印的行的所有第一个值。 before is set to the size of printed, before the new element has been added. 将before设置为在添加新元素之前的打印尺寸。

When printed.add(row[0]); printed.add(row[0]);printed.add(row[0]); is called one of two things happen: The value is already in the printed, so the size of printed doesn't change. 被称为发生以下两种情况之一:值已经在打印文件中,因此打印的大小不会改变。 The value is not in printed so the element is added. 该值未打印,因此添加了元素。

The check printed.size()>before will be true only if the element has not been printed before. 仅在之前未打印过元素的情况下,“ printed.size()>before ”检查printed.size()>before为真。

This is one way to do it. 这是做到这一点的一种方法。 A Set cannot have repeating elements. Set不能包含重复元素。

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

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

The following sample program will do the work: 以下示例程序将完成此工作:

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

The output is: 输出为:

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

Explanation: It makes use of Set which does not allow duplicates. 说明:它使用Set ,不允许重复。 When we add an element to the set it will return boolean which is true if recently added element is unique. 当我们向集合中添加元素时,它将返回布尔值,如果最近添加的元素是唯一的,则返回true。 In that case we will print that particular row from array. 在这种情况下,我们将打印数组中的特定行。

Assuming the values are grouped like shown in the example, here's the simplest way of doing it: 假设值如示例中所示进行分组,这是最简单的方法:

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

Output 输出量

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