java 使用 for 循环将一个数组复制到另一个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16728707/
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
Using a for loop to copy one array to another
提问by Shotgam
I've been working with arrays and I'm trying to find a way to copy one array to another solely with a for loop (I've seen things like arrayCopy and clone in other threads, but for this exercise I need to just copy from one array to another using a for loop).
我一直在使用数组,我正在尝试找到一种方法来仅使用 for 循环将一个数组复制到另一个数组(我已经在其他线程中看到了诸如 arrayCopy 和 clone 之类的东西,但是对于这个练习,我只需要复制使用 for 循环从一个数组到另一个数组)。
Right now my code executes, but just spits this out: "[D@133c5982". The code I'm using is:
现在我的代码正在执行,但只是吐出:“[D@133c5982”。我使用的代码是:
public class sorted
{
public static void main(String[] args)
{
int [] unSortedArray = {8,12,6,5,19};
System.out.println(copyArray(unSortedArray));
//System.out.println(sortedArray(unSortedArray));
}
public static int[] copyArray(int[] array)
{
int [] copyArray;
copyArray = new int[array.length];
for (int i = 1; i < array.length; i++)
{
copyArray[i] = array[i];
}
return copyArray;
}
回答by ajduke
First of all, you are notcopying whole array, as you starting your index with 1
首先,您不是在复制整个数组,因为您从 1 开始索引
as in your code
就像在你的代码中一样
for (int i = 1; i < array.length; i++)
{
copyArray[i] = array[i];
}
start the index with 0
索引以 0 开头
and second you can make use of Arrays.deepToString(array)
or Arrays.toString(array)
to print array in readable format
其次,您可以使用Arrays.deepToString(array)
或Arrays.toString(array)
以可读格式打印数组
回答by chopchop
Your code is OK, it does copy the array. When you do System.out.println
it prints out the default implementation of the array's toString()
method). To see that it is indeed copied, you can do
您的代码没问题,它确实复制了数组。当你这样做时,System.out.println
它会打印出数组toString()
方法的默认实现)。要查看它确实被复制,您可以执行
for (int i = 0; i<unsortedArray.length; i++) {
System.out.println(unsortedArray[i]);
}
for (int i = 0; i<copiedArray.length; i++) {
System.out.println(copiedArray[i]);
}
EDIT: see the comments for what you code actually prints out
编辑:查看您的代码实际打印出来的注释
回答by Mzn
copyArray
returns an array and that's what you are printing. What you see is the Java Virtual Machine way of representing objects as strings. @12345 is the object's ID.
copyArray
返回一个数组,这就是您要打印的内容。您看到的是将对象表示为字符串的 Java 虚拟机方式。@12345 是对象的 ID。
There is nothing wrong with the code provided, except a } at the end (possibly omitted while copying).
提供的代码没有任何问题,除了末尾的 } (复制时可能省略)。
回答by ellak
When printing you are using the arrays default toString
which doesn't print the content of the Strings.
打印时,您使用的是数组默认值toString
,它不打印字符串的内容。
Since Java 1.5 you can use Arrays.toString(array)
to get a nice String representation of the array.
从 Java 1.5 开始,您可以使用它Arrays.toString(array)
来获得数组的一个很好的 String 表示形式。
Use this with System.out.println
to get a nice printout.
使用它System.out.println
可以获得漂亮的打印输出。
回答by Adarsh
What you have done is correct. But if you print an array using System.out.println(copyArray(unSortedArray));
it only prints out the location of the array. Instead, try printing each element of the array in a for loop.
你所做的是正确的。但是如果你使用System.out.println(copyArray(unSortedArray));
它打印一个数组,它只会打印出数组的位置。相反,尝试在 for 循环中打印数组的每个元素。
for (int i = 1; i < array.length; i++)
{
system.out.println(array[i]);
}
//Arrays.toString(<arrayname>) can also be used in place of the for loop.
Also, array index starts at 0. So, your method should be as follows.
此外,数组索引从 0 开始。因此,您的方法应如下所示。
public static int[] copyArray(int[] array)
{
int [] copyArray;
copyArray = new int[array.length];
for (int i = 0; i < array.length; i++)
{
copyArray[i] = array[i];
}
return copyArray;
回答by KhAn SaAb
try this
试试这个
package naveed.workingfiles;
import java.util.Arrays;
public class Array
{
public static void main(String[] args)
{
int [] unSortedArray = {8,12,6,5,19};
int [] unSortedCopyArray =new int [unSortedArray.length];
//System.out.println(sortedArray(unSortedArray));
for(int i=0;i<unSortedArray.length;i++)
{
unSortedCopyArray[i]=unSortedArray[i];
}
System.out.println(Arrays.toString(unSortedArray));//exist array
System.out.println(Arrays.toString(unSortedCopyArray));//copied array
}
}