![](/img/trans.png)
[英]Printing message only once after iterating through the first column of a 2d array
[英]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.