Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何将数组中的元素移动到另一个数组?_Java_Arrays_Element_Move - Fatal编程技术网

Java-如何将数组中的元素移动到另一个数组?

Java-如何将数组中的元素移动到另一个数组?,java,arrays,element,move,Java,Arrays,Element,Move,我制定了以下代码: Integer []array=new Integer[999]; Integer []dump=new Integer[999]; for( int i=0;xconv!=0;++i){//ignore this array[i]=xconv%2;//ignore this xconv=xconv/2;//ignore this System.out.println("array= "+array[i]+" xconv= "+xconv);

我制定了以下代码:

Integer []array=new Integer[999];
   Integer []dump=new Integer[999];  
    for( int i=0;xconv!=0;++i){//ignore this

      array[i]=xconv%2;//ignore this
xconv=xconv/2;//ignore this
 System.out.println("array= "+array[i]+" xconv= "+xconv);//ignore this

   if(array[i]==null){//<<<<<<look here

   array[i]=ArrayUtils.toArray(dump, i);//<<<<<<<<<<<<<look here

 }
}
System.out.println(Arrays.toString(array));//<<<<<<look here
Integer[]数组=新整数[999];
整数[]转储=新整数[999];
对于(inti=0;xconv!=0;++i){//忽略此项
数组[i]=xconv%2;//忽略此
xconv=xconv/2;//忽略这个
System.out.println(“array=“+array[i]+”xconv=“+xconv);//忽略此项

如果(array[i]==null){/这不是toArray的工作方式。请检查JavaDoc:


您似乎不了解什么是数组或它们是如何工作的。
数组是一种固定长度的数据结构。
该数组中的每个索引可能包含一个值,也可能为空

听起来您正在寻找的是一个数据结构,它是一个动态调整大小的数据结构(在添加和删除元素时它会增长和收缩)

当您在
列表上迭代时,您将只看到它当前包含的元素,而不是数组,在数组中,您需要遍历它的长度并检查每个条目,以查看其中是否有值

如果您已经习惯于使用数组进行计算,您仍然可以轻松地将它们转换为
列表
,以便显示

与此相反:

    System.out.println(Arrays.toString(array));//<<<<<<look here

只需打印您关心的范围:

    int[] array = new int[999];
    int i;
    for (i = 0; xconv != 0; ++i)
    {
        array[i] = xconv % 2;
        xconv = xconv / 2;
        System.out.println("array= " + array[i] + " xconv= " + xconv);
    }
    System.out.println(Arrays.toString(Arrays.copyOf(array, i)));
更好的方法是使用
ArrayList

    List<Integer> array = new ArrayList<>();
    for (int i = 0; xconv != 0; ++i)
    {
        array.add(xconv % 2);
        xconv = xconv / 2;
        System.out.println("array= " + array.get(i) + " xconv= " + xconv);
    }
    System.out.println(array.toString());
List array=new ArrayList();
对于(int i=0;xconv!=0;++i)
{
array.add(xconv%2);
xconv=xconv/2;
System.out.println(“array=“+array.get(i)+”xconv=“+xconv”);
}
System.out.println(array.toString());

您无法移动空元素,它们为空,因为那里没有元素。
新整数[999]
创建了一个包含999个对
整数
对象的引用的数组,所有引用都以默认值
null
开头。你确定要一个
整数
数组而不是
int
数组吗?谢谢你的解释,但是该代码对我不起作用,空值仍会持续显示。我要将其更改为那就列个清单吧。再次感谢!是的,我现在发现了xD
    List<Integer> array = new ArrayList<>();
    for (int i = 0; xconv != 0; ++i)
    {
        array.add(xconv % 2);
        xconv = xconv / 2;
        System.out.println("array= " + array.get(i) + " xconv= " + xconv);
    }
    System.out.println(array.toString());