使用 for 循环从二维数组中删除符合条件的行
Removing a row matching a condition from a 2D array using for loop
我有问题。我试图从符合特定条件的二维数组中删除一行。
我之前使用
创建了一个二维数组
String [][] T = new String [line][column];
用户可以选择行数和列数,并用他选择的值填充 table。其中一列必须包含商品价格,这些价格将与用户给出的价格限制进行比较,低于价格的每个商品都必须从二维数组(行)中删除。有人告诉我使用数组。但是在这里我需要使用“for循环”和一个2D table.
到目前为止,这就是我所拥有的“移除部分”
public static String[][] deleterow(String[][]T, int priceLimit)
{
// count number of lines matching the condition (used later to redefine the new size)
int count=0;
int priceItem;
for (int i=0; i<T.length;i++)
{
for (int j=3; j<T[i].length;j++) // 4th colomn should contains prices
{
priceItem = Integer.parseInt(T[i][j]);
if (priceItem< priceLimit)
{
count=count+1;
}
}
}
String [][]T3 = new String [T.length-count][];
for (int i=0; i<T.length;i++)
{
for (int j=0; j<T[i].length;j++)
{
priceItem = Integer.parseInt(T[i][j]);
if(priceItem < priceLimit)
{
T3[i][j]=T[i+1][j];
}
else
{
T3[i][j]=T[i][j];
}
}
}
System.out.println(count+" items deleted from the table");
不幸的是,我在此处从 String 到 Int 的解析中遇到问题,因此代码不会继续。有什么可以改进的?
这是一个简单的方法,如果第 4 列小于 priceLimit,则将行转换为空值,调用该方法后您将得到一个包含空值的数组,但您可以在此处查找 Remove Null Value from String array in java弄清楚如何从数组中删除空值。
public static void main(String[] args) {
int lines = 4;
int columns = 4;
String[][] twoDArray = new String [lines][columns];
for (int i = 0; i < lines; i++) {
for (int j = 0; j < columns; j++) {
twoDArray[i][j] = "test";
}
}
twoDArray[0][3] = "5";
twoDArray[1][3] = "10";
twoDArray[2][3] = "15";
twoDArray[3][3] = "20";
System.out.println(Arrays.toString(twoDArray));
deleteMatchingRow(twoDArray, 16);
System.out.println(Arrays.toString(twoDArray));
}
public static void deleteMatchingRow (String[][] twoDArray, int priceLimit) {
for (int i = 0; i < twoDArray.length; i++) {
if (Integer.parseInt(twoDArray[i][3]) < priceLimit) {
twoDArray[i] = null;
}
}
}
现在关于你的异常 java.lang.NumberFormatException
你只需要确保在每一行中,第 4 列确实是一个可解析的整数而不是随机字符串。
请注意,在 main 方法中,我如何用 "test"
填充所有两个 D 数组,然后将每行的第 4 列重新分配给字符串数字,以便它们可以被 Integer.parseInt()
解析
我有问题。我试图从符合特定条件的二维数组中删除一行。 我之前使用
创建了一个二维数组String [][] T = new String [line][column];
用户可以选择行数和列数,并用他选择的值填充 table。其中一列必须包含商品价格,这些价格将与用户给出的价格限制进行比较,低于价格的每个商品都必须从二维数组(行)中删除。有人告诉我使用数组。但是在这里我需要使用“for循环”和一个2D table.
到目前为止,这就是我所拥有的“移除部分”
public static String[][] deleterow(String[][]T, int priceLimit)
{
// count number of lines matching the condition (used later to redefine the new size)
int count=0;
int priceItem;
for (int i=0; i<T.length;i++)
{
for (int j=3; j<T[i].length;j++) // 4th colomn should contains prices
{
priceItem = Integer.parseInt(T[i][j]);
if (priceItem< priceLimit)
{
count=count+1;
}
}
}
String [][]T3 = new String [T.length-count][];
for (int i=0; i<T.length;i++)
{
for (int j=0; j<T[i].length;j++)
{
priceItem = Integer.parseInt(T[i][j]);
if(priceItem < priceLimit)
{
T3[i][j]=T[i+1][j];
}
else
{
T3[i][j]=T[i][j];
}
}
}
System.out.println(count+" items deleted from the table");
不幸的是,我在此处从 String 到 Int 的解析中遇到问题,因此代码不会继续。有什么可以改进的?
这是一个简单的方法,如果第 4 列小于 priceLimit,则将行转换为空值,调用该方法后您将得到一个包含空值的数组,但您可以在此处查找 Remove Null Value from String array in java弄清楚如何从数组中删除空值。
public static void main(String[] args) {
int lines = 4;
int columns = 4;
String[][] twoDArray = new String [lines][columns];
for (int i = 0; i < lines; i++) {
for (int j = 0; j < columns; j++) {
twoDArray[i][j] = "test";
}
}
twoDArray[0][3] = "5";
twoDArray[1][3] = "10";
twoDArray[2][3] = "15";
twoDArray[3][3] = "20";
System.out.println(Arrays.toString(twoDArray));
deleteMatchingRow(twoDArray, 16);
System.out.println(Arrays.toString(twoDArray));
}
public static void deleteMatchingRow (String[][] twoDArray, int priceLimit) {
for (int i = 0; i < twoDArray.length; i++) {
if (Integer.parseInt(twoDArray[i][3]) < priceLimit) {
twoDArray[i] = null;
}
}
}
现在关于你的异常 java.lang.NumberFormatException
你只需要确保在每一行中,第 4 列确实是一个可解析的整数而不是随机字符串。
请注意,在 main 方法中,我如何用 "test"
填充所有两个 D 数组,然后将每行的第 4 列重新分配给字符串数字,以便它们可以被 Integer.parseInt()