Java 将数组中的所有元素乘以外部数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19608578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiplying all elements in an array by an outside number?
提问by prodo
I need to multiple all the values in an array by 3000 which in turn would create a new array that I will use to subtract from another array. I've tried to create a separate method that would do that for me but all I got back in the multiplied array was a bunch of numbers and symbols strangely?
我需要将数组中的所有值乘以 3000,这又会创建一个新数组,我将使用该数组从另一个数组中减去。我试图创建一个单独的方法来为我做这件事,但我在乘法数组中得到的只是一堆奇怪的数字和符号?
here is the code that I wrote
这是我写的代码
public static void main(String[] args)
{
int numberOfTaxpayers = Integer.parseInt(JOptionPane.showInputDialog("Enter how many users you would like to calculate taxes for: ");
int[] usernumChild = new int[numberOfTaxPayers];
for (int i = 0; i < usernumChild.length; i++)
{
usernumChild[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter number of children for user "+ (i+1) +": "));
}//this for loop finds out the number of children per user so we can later multiply each input by 3000 to create an array that determine dependency exemption for each user
int[] depndExemp = multiply(usernumChild, 3000);//this was the calling of the multiply method... somewhere here is the error!!
}//end main method
public static int[] multiply(int[] children, int number)
{
int array[] = new int[children.length];
for( int i = 0; i < children.length; i++)
{
children[i] = children[i] * number;
}//end for
return array;
}//this is the method that I was shown in a previous post on how to create return an array in this the dependency exemption array but when I tested this by printing out the dependency array all I received were a jumble of wrong numbers.
采纳答案by Tyler
In your example you're multiplying your children array but returning your new array. You need to multiply your new array by your children array.
在您的示例中,您正在乘以您的 children 数组,但返回您的新数组。您需要将新数组乘以子数组。
1 public static int[] multiply(int[] children, int number)
2 {
3 int array[] = new int[children.length];
4 for( int i = 0; i < children.length; i++)
5 {
6 array[i] = children[i] * number;
7 }//end for
8 return array;
9 }
The reason you're getting strange symbols is because you are returning uninitialized values. The array itself is allocated at line 3 but at this point each index of the array has not been initialized so we don't really know what values are in there.
你得到奇怪符号的原因是因为你返回了未初始化的值。数组本身在第 3 行分配,但此时数组的每个索引还没有初始化,所以我们并不真正知道那里有什么值。
回答by Alexis C.
In your second for loop it should be :
在您的第二个 for 循环中,它应该是:
for(int i = 0; i < children.length; i++){
array[i] = children[i] * number;
}//end for
Also make sure that all values of children[i]
are inferior than ((2^31 - 1)/number) +1
还要确保所有的值children[i]
都低于((2^31 - 1)/number) +1
回答by Prabhakaran Ramaswamy
You need to Change
你需要改变
children[i] = children[i] * number;
to
到
array[i] = children[i] * number;
回答by Petr Mensik
You really don't have to create new array in your method (and you are also returning the old one without any change). So just do
您真的不必在您的方法中创建新数组(并且您也无需任何更改即可返回旧数组)。所以就做
public static int[] multiply(int[] children, int number) {
for(int i = 0; i < children.length; i++) {
children[i] = children[i] * number;
}
return children;
}
回答by Josh M
If I understand your question correctly:
如果我正确理解你的问题:
children[i] = children[i] * number;
Should be changed to
应该改为
array[i] = children[i] * number;
Considering you are returning array
, not children
.
考虑到您正在返回array
,而不是children
。
回答by c0der
Using Java 8 streams it can be as simple as:
使用 Java 8 流可以很简单:
public static int[] multiply(int[] children, int number) {
return Arrays.stream(children).map(i -> i*number).toArray();
}