java 在java方法中正确输入int[]?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16267438/
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
Correctly input int[] in java method?
提问by user2133735
This method insert takes as input int[] array
, int element
, and int index
, which inserts element
into the index
position of array
. Since an array is not resizable, the method shifts every element over to the right of the array by one. The element at the end is removed from the array. The method returns void
.
该方法 insert 将, 和当作输入int[] array
,插入到 的位置。由于数组不可调整大小,该方法将每个元素移到数组的右侧一个。末尾的元素从数组中删除。该方法返回。int element
int index
element
index
array
void
public class ShiftElements {
public static void insert(int[] array, int element, int index)
{
for (int i = array.length-1; i > index; i--)
{
array[i] = array[i-1];
}
array[index] = element;
}
}
To test if this method works, I changed the return type to int[]
and wrote a main method to print array
:
为了测试这个方法是否有效,我将返回类型更改为int[]
并编写了一个 main 方法来打印array
:
public class ShiftElements {
public static int[] insert(int[] array, int element, int index)
{
for (int i = array.length-1; i > index; i--)
{
array[i] = array[i-1];
}
return array;
}
public static void main(String[] args) {
System.out.print(insert(4,5,3));
}
}
I am having problems, getting this print statement to work. It's probably something simple, but I've been up for two days studying for finals so I'm pretty braindead.
我遇到了问题,让这个打印语句起作用。这可能是一些简单的事情,但我已经为期末考试学习了两天,所以我很脑残。
I'm pretty sure I called the insert
method just fine, but I think my issue is that I'm not properly inputting the type int[]
. I'm not sure how I'm supposed to do this.
我很确定我调用了这个insert
方法很好,但我认为我的问题是我没有正确输入 type int[]
。我不确定我该怎么做。
回答by Vishal K
I guess what you are looking for is :
我猜你要找的是:
int[] i = new int[5];
System.out.print(java.util.Arrays.toString(insert(i,5,3)));
Apart from the use of java.util.Arrays.toString
method Watch out the parameters that you are passing in insert
method:
first parameter should be an array of int
. But you are passing an int
.
除了java.util.Arrays.toString
方法的使用注意你在insert
方法中传递的参数:第一个参数应该是int
. 但是你正在传递一个int
.
回答by Thierry
1) The first parameter of insert is an array of int, not an int. You must call :
1)insert的第一个参数是int数组,不是int。您必须致电:
insert(new int[]{4},5,3)
2) you can't print an array : so use :
2)你不能打印一个数组:所以使用:
println(Arrays.toString(insert(...)))
回答by Joshua
System.out.print(insert(4,5,3));
That statement does not pass a array of integers, you only passed and integer 4. You would need to pass, as your function is defined, int[], int, int.
该语句不传递整数数组,您只传递整数 4。您需要传递,因为您的函数已定义,int[]、int、int。
System.out.print(insert(new int[4],5,3));
回答by Ben Barkay
You made a call to insert(int,int,int)
eventhough your signature for insert is insert(int[],int,int)
. For instance, calling insert with an array literal would be like so:
insert(int,int,int)
即使您的插入签名是 ,您也拨打了电话insert(int[],int,int)
。例如,使用数组文字调用 insert 将如下所示:
insert(new int[]{1,2,4,5}, 3, 2);
Additionally, System.arraycopyis a much faster and elegant way to copy arrays. You also seem to have forgotten to set the element at its index within your second example:
此外,System.arraycopy是一种更快、更优雅的复制数组的方式。您似乎也忘记在第二个示例中将元素设置为其索引:
public static int[] insert(int[] array, int element, int index)
{
// Use arraycopy to shift all the elements by one, running over the last index
System.arraycopy(array, index, array, index+1, array.length-index-1);
// Set the appropriate index in the array to the specified value
array[index]=element;
return array;
}
If you then run the following, you will get your expected output:
如果然后运行以下命令,您将获得预期的输出:
int[] array = {1, 2, 4, 5};
insert(array,3,2); // array is now {1,2,3,4}
System.out.println(java.util.Arrays.toString(array));
回答by Sushim Mukul Dutta
If you are looking to print the array then the most laymen procedure is to return the particular array and loop throw it to print its element. eg:
如果您要打印数组,那么最外行的过程是返回特定数组并循环抛出它以打印其元素。例如:
public static void main(String[] args) {
int []solution= insert(new int[4],5,3);
for(int i=0;i<solution.length;i++)
{
System.out.println(solution[i]);
}
}