将2d数组的元素乘以另一个2d数组JAVA的元素的方法

时间:2021-12-11 21:30:22

I'm having trouble multiplying the elements of a 2d string array by the element of another. Heres the code:

我无法将2d字符串数组的元素乘以另一个元素。继承人代码:

public static String updateString(String[][] array, String[] prices)
{
    String [][] newArray = new String[array.length][]; 
    for(int row = 0; row < prices.length; row++)
    {
        if (array[row][0].equals(prices[row]))
        {
            for(int i = 0; row <array.length; row++)
            {
                newArray[row][i+1] = array[row][i+1] * prices[i+1];
            }
        }
    }
}

Here are what the files look like:

以下是文件的外观:

array:
Omaha,104,1218,418,216,438,618,274,234,510,538,740,540
Saint Louis,72,1006,392,686,626,670,204,286,236,344,394,930
Des Moines,116,1226,476,330,444,464,366,230,602,260,518,692
Chicago,408,948,80,472,626,290,372,282,488,456,376,580
Kansas City,308,1210,450,234,616,414,500,330,486,214,638,586
Austin,500,812,226,470,388,488,512,254,210,388,738,686
Houston,454,1086,430,616,356,534,218,420,494,382,476,846
New Orleans,304,1278,352,598,288,228,532,418,314,496,616,882

prices array:
Omaha,7.5
Saint Louis,10.5
Des Moines,8.5
Chicago,11.5
Kansas City,12.5
Austin,10.75
Houston,12.5
New Orleans,9.25

As you can see, the first column of each array lists the city, so if the cities match up, I need the 1st array's elements multiplied by omaha(7.5).

如您所见,每个数组的第一列列出了城市,因此如果城市匹配,我需要第一个数组的元素乘以奥马哈(7.5)。

4 个解决方案

#1


0  

There are two problems with your inner loop.

内循环有两个问题。

1) You need to increment 'i' at some point. You need to have in your loop:

1)你需要在某个时刻递增'i'。你需要在循环中:

i++;

Currently, you are traversing the rows not the columns in your inner loop, get rid of the:

目前,您正在遍历行而不是内循环中的列,摆脱:

row++;

2) You never initialize the columns of newArray. Since you are using a static 2D array you should have a max number of columns shown here as MAX_WIDTH.

2)你永远不会初始化newArray的列。由于您使用的是静态2D数组,因此此处显示的最大列数应为MAX_WIDTH。

String [][] newArray = new String[array.length][MAX_WIDTH];

As the others mentioned, you definitely need to convert from String to double before you can do any 'math' on the variables. Here Java automatically casts Double (class) to double (data structure).

正如其他人提到的那样,在对变量进行任何“数学运算”之前,你肯定需要从String转换为double。这里Java自动将Double(class)转换为double(数据结构)。

try {
    double val = Double.parseDouble(string);
} catch (NumberFormatException e) {
    e.printStackTrace();
}

#2


1  

IF you have no choice but to use Strings where numbers should be the best choice, then try to convert your strings to numbers like this -

如果您别无选择,只能使用数字应该是最佳选择的字符串,那么尝试将字符串转换为这样的数字 -

String str = "22.43";
try{
    double str = Double.parseDouble(str);
}catch(NumberFormatException nfe){
    nfe.printStackTrace(); 
}

This is just an example. You might want to see the disadvantages of using print stack trace - Why is exception.printStackTrace() considered bad practice?

这只是一个例子。您可能希望看到使用打印堆栈跟踪的缺点 - 为什么exception.printStackTrace()被认为是不好的做法?

#3


0  

Try casting the values to either an integer or double type. Currently you are attempting to multiply string values.

尝试将值转换为整数或双精度类型。目前您正在尝试将字符串值相乘。

#4


0  

You are multiplying Strings, first convert them to double and then multiply First you are not returning any thing and also Change your return type from String to String [] [] as you are returning 2d Array

你正在乘以字符串,首先将它们转换为double然后乘以首先你没有返回任何东西,并且当你返回2d数组时也将你的返回类型从String更改为String [] []

Here is the code

这是代码

public static String [][]updateString(String[][] array, String[] prices)
    {
        String [][] newArray = new String[array.length][]; 
        for(int row = 0; row < prices.length; row++)
        {
            if (array[row][0].equals(prices[row]))
            {
                for(int i = 0; i<array.length; i++)
                {
                    Double d=Double.parseDouble(array[row][i+1]) * Double.parseDouble(prices[i+1]);
                    newArray[row][i+1] = d.toString();
                }
            }
        }
        return newArray;
    }
}

Your inner loop should be something like I have done in the code depending on what you want

你的内部循环应该像我在代码中所做的那样取决于你想要的东西

#1


0  

There are two problems with your inner loop.

内循环有两个问题。

1) You need to increment 'i' at some point. You need to have in your loop:

1)你需要在某个时刻递增'i'。你需要在循环中:

i++;

Currently, you are traversing the rows not the columns in your inner loop, get rid of the:

目前,您正在遍历行而不是内循环中的列,摆脱:

row++;

2) You never initialize the columns of newArray. Since you are using a static 2D array you should have a max number of columns shown here as MAX_WIDTH.

2)你永远不会初始化newArray的列。由于您使用的是静态2D数组,因此此处显示的最大列数应为MAX_WIDTH。

String [][] newArray = new String[array.length][MAX_WIDTH];

As the others mentioned, you definitely need to convert from String to double before you can do any 'math' on the variables. Here Java automatically casts Double (class) to double (data structure).

正如其他人提到的那样,在对变量进行任何“数学运算”之前,你肯定需要从String转换为double。这里Java自动将Double(class)转换为double(数据结构)。

try {
    double val = Double.parseDouble(string);
} catch (NumberFormatException e) {
    e.printStackTrace();
}

#2


1  

IF you have no choice but to use Strings where numbers should be the best choice, then try to convert your strings to numbers like this -

如果您别无选择,只能使用数字应该是最佳选择的字符串,那么尝试将字符串转换为这样的数字 -

String str = "22.43";
try{
    double str = Double.parseDouble(str);
}catch(NumberFormatException nfe){
    nfe.printStackTrace(); 
}

This is just an example. You might want to see the disadvantages of using print stack trace - Why is exception.printStackTrace() considered bad practice?

这只是一个例子。您可能希望看到使用打印堆栈跟踪的缺点 - 为什么exception.printStackTrace()被认为是不好的做法?

#3


0  

Try casting the values to either an integer or double type. Currently you are attempting to multiply string values.

尝试将值转换为整数或双精度类型。目前您正在尝试将字符串值相乘。

#4


0  

You are multiplying Strings, first convert them to double and then multiply First you are not returning any thing and also Change your return type from String to String [] [] as you are returning 2d Array

你正在乘以字符串,首先将它们转换为double然后乘以首先你没有返回任何东西,并且当你返回2d数组时也将你的返回类型从String更改为String [] []

Here is the code

这是代码

public static String [][]updateString(String[][] array, String[] prices)
    {
        String [][] newArray = new String[array.length][]; 
        for(int row = 0; row < prices.length; row++)
        {
            if (array[row][0].equals(prices[row]))
            {
                for(int i = 0; i<array.length; i++)
                {
                    Double d=Double.parseDouble(array[row][i+1]) * Double.parseDouble(prices[i+1]);
                    newArray[row][i+1] = d.toString();
                }
            }
        }
        return newArray;
    }
}

Your inner loop should be something like I have done in the code depending on what you want

你的内部循环应该像我在代码中所做的那样取决于你想要的东西