gpt4 book ai didi

Java - 添加行有更有效的方法吗?

转载 作者:行者123 更新时间:2023-11-30 04:23:51 25 4
gpt4 key购买 nike

下面是代码,我只是想知道其他人是否以不同的方式编码。也许我可以做一些小改变。提前致谢!

public class addRow {

public static int[][] insert(int [][] a, int [] row, int index){

int [][] x = new int[a.length+1][a.length];
int [] temp;

for(int i=0; i<x.length-1; i++)
{
x[i] = a[i];

if(i == index)
{
temp = a[i];
x[i] = row;
x[i+1] = temp;
}
}
return x;
}

public static void main(String[] args){
int[][] a = {{1,2,3},{4,5,6},{10,11,12}};
int[] row = {7,8,9};

int [][] b = insert(a,row ,2);


for(int r=0; r < b.length; r++){
for(int c=0;c< b[r].length; c++){
System.out.print(b[r][c] + " ");
}System.out.println();
}
}
}

最佳答案

System.arraycopy 就是您的答案。

http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29

它使用 native 代码直接复制内存中的数组,从而提高效率。

关于Java - 添加行有更有效的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16351217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com