JAVA |仅对2D数组的非空值进行分组
在JAVA中,我有一个二维数组,如下所示:JAVA |仅对2D数组的非空值进行分组,java,arrays,Java,Arrays,在JAVA中,我有一个二维数组,如下所示: sl_no plan_names Column3 Column4 Column5 Column6 1 abc product1 2 stu 3 vwx 4 yza product2 5 bcd
sl_no plan_names Column3 Column4 Column5 Column6
1 abc product1
2 stu
3 vwx
4 yza product2
5 bcd
6 efg
7 nop
8 qrs product3 product4 product5
9 tuv
sl_no plan_names products
1 abc product1
2 yza poduct2
3 qrs product3
4 qrs product4
5 qrs product5
现在,希望在单独的数组中列出仅包含非空值的产品,如下所示:
sl_no plan_names Column3 Column4 Column5 Column6
1 abc product1
2 stu
3 vwx
4 yza product2
5 bcd
6 efg
7 nop
8 qrs product3 product4 product5
9 tuv
sl_no plan_names products
1 abc product1
2 yza poduct2
3 qrs product3
4 qrs product4
5 qrs product5
如果有人指导我如何做到这一点,这将对我非常有帮助
谢谢,,
Praveen您可以使用面向对象的方法,将各种数据列存储到一个对象中,并创建一个对象数组,而不是使用2D数组来存储各种类型的数据 输出:
--------------------------------------------------------------
s_no plan_names Column3 Column4 Column5
1 abc product1 null null
2 stu null null null
3 vwx null null null
4 yza product2 null null
5 bcd product3 null product4
--------------------------------------------------------------
s_no plan_names Column3 Column4 Column5
1 abc product1 null null
2 yza product2 null null
3 bcd product3 null product4
4 bcd product3 null product4
创建一个类来存储计划
class Plan
{
private String planName;
private String[] columns;
public Plan(String planName){
this.planName = planName;
columns = new String[3];
}
public boolean allColumnIsNull(){
for(String s : columns)
if(s != null)
return false;
return true;
}
public void setColumn(String val, int colNum){
if(colNum >= 0 && colNum < columns.length)
columns[colNum] = val;
}
public String[] getColumns(){
return columns;
}
public String toString(){
return (String.format("%-15s", planName) + String.format("%-15s", columns[0]) +
String.format("%-15s", columns[1]) + String.format("%-15s", columns[2]));
}
}
检索非空数据的方法:
public static ArrayList<Plan> getNonEmptyPlans(Plan[] plans){
ArrayList<Plan> list = new ArrayList<Plan>();
for(Plan p : plans)
if(!p.allColumnIsNull())
for(String s : p.getColumns())
if(s != null)
list.add(p);
return list;
}
publicstaticarraylistgetnonemptyplans(计划[]计划){
ArrayList=新建ArrayList();
用于(计划p:计划)
如果(!p.allColumnIsNull())
for(字符串s:p.getColumns())
如果(s!=null)
增加(p);
退货清单;
}
试试这个
// Original Array which stores all data
String arr_original[][] = new String[9][6];
// Array where you have to copy
String arr2[][] = new String[4][3];
// Separate index for second array as you don't want null values
// This index will increment only when product != null
int row2 = 0;
// To store Product Name of each row of original Array
String product = "";
// Iterate through all the rows of original array
for (int row = 0; row <= arr_original.length; row++) {
// Now we need to assign the entry in second array only when product
// is not null
// Check if products != NULL
if (arr_original[row][2] != null
|| arr_original[row][3] != null
|| arr_original[row][4] != null
|| arr_original[row][5] != null) {
// Assign first 2 columns to second table
arr2[row2][0] = arr_original[row][0];
arr2[row2][1] = arr_original[row][1];
// Find the product from the other 4 columns
for (int col = 2; col <= 5; col++) {
if (arr_original[row][col] != null) {
// if product != null, assign this product
product = arr_original[row][col];
// Since NOT null product is found, break the loop
break;
}
}
// Assign product to 3rd column
arr2[row2][2] = product;
row2++;
}
}
//存储所有数据的原始数组
字符串arr_original[][]=新字符串[9][6];
//必须复制的数组
字符串arr2[][]=新字符串[4][3];
//第二个数组的单独索引,因为您不需要空值
//仅当产品!=无效的
int row2=0;
//存储原始数组中每行的产品名称
字符串乘积=”;
//遍历原始数组的所有行
对于(int row=0;row除了在这里发布之外,您还有什么尝试吗?@P.K.S为什么要使用2D数组来存储不同的东西(平面名称,columnX)?您可以使用类和对象吗?@P.K.S请参阅下面我的工作解决方案。@Suresh Atta,请在下面找到我的代码:DataArray=new String[totalRows][3];int K=0;DataArray[0][0]=“sl_no”DataArray[0][1]=“plan_names”DataArray[0][2]=“products”;for(int i=1;这里遇到的问题是,新数组的第一个维度是从原始数组的第一个维度派生出来的–这里是totalRows。在新数组中筛选并列出记录后,剩余记录(totalRows之外)将更新为“null”如下。sl_无计划_命名产品1 abc产品1 yza poduct2 3 qrs产品3 4 qrs产品4 5 qrs产品5空空空
感谢您的回复,将尝试confirm@P.K.S如果我的公司de帮助,您可以接受我的答案;)事实上,这是一种更适合处理数据的方法,而不是使用2D数组。@Anandh,谢谢!我也派生了与您的代码类似的代码!但是,当我们使用[arr_original.lengt],在新数组上获取意外的空值,如下所示。sl_无计划_命名产品1 abc产品1 2 yza poduct2 3 qrs产品3 4 qrs产品4 5 qrs产品5空值空值
// Original Array which stores all data
String arr_original[][] = new String[9][6];
// Array where you have to copy
String arr2[][] = new String[4][3];
// Separate index for second array as you don't want null values
// This index will increment only when product != null
int row2 = 0;
// To store Product Name of each row of original Array
String product = "";
// Iterate through all the rows of original array
for (int row = 0; row <= arr_original.length; row++) {
// Now we need to assign the entry in second array only when product
// is not null
// Check if products != NULL
if (arr_original[row][2] != null
|| arr_original[row][3] != null
|| arr_original[row][4] != null
|| arr_original[row][5] != null) {
// Assign first 2 columns to second table
arr2[row2][0] = arr_original[row][0];
arr2[row2][1] = arr_original[row][1];
// Find the product from the other 4 columns
for (int col = 2; col <= 5; col++) {
if (arr_original[row][col] != null) {
// if product != null, assign this product
product = arr_original[row][col];
// Since NOT null product is found, break the loop
break;
}
}
// Assign product to 3rd column
arr2[row2][2] = product;
row2++;
}
}