arraycopy(Object src, int src_position, Object dst, int dst_position, int length) Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. 明白?
sourceLength*2把数组长度扩大2倍
谢谢0legend 还有一个问题,我能在新的数组里面赋值吗? 我是说在这里 System.arraycopy(source, 2002, result, 2003, sourceLength); //Add new data for array return result; 这样是不是把原来的数组也给改变了呢?
再请问,下面twoDimStr 的二维数组是多少? twoDimStr[?][?] 是twoDimStr[5][10]?int[] dimStr = {5, 10}; String[][] twoDimStr = (String[][]) Array.newInstance(String.class,dimStr);System.out.println("dimStr:"); for ( k = 0; k < Array.getLength(dimStr); k++) System.out.println(dimStr[k]);System.out.println("twoDimStr:"); for ( k = 0; k < Array.getLength(twoDimStr); k++) for ( i=0;i< Array.getLength(twoDimStr); i++) System.out.println(twoDimStr[k][i]);
public class LeapYear { public static void main(String args[]) { int[] nYear = {1998, 1999, 2000, 2001}; int[] nOtherYears = {18, 19, 20, 21};
System.arraycopy(nYear, 2, nOtherYears, 2, 2); for (int i=0;i< Array.getLength(nOtherYears);i++ ) { System.out.println(nOtherYears[i]+" is bissextile year"); } } } // result is : nYear = {1998, 1999, 2000, 2001}; nOtherYears = {18, 19, 2000, 2001}; 2> 是twoDimStr[5][10]?yes
我的想法不是这样的. 我的想法是在COPY 一个新的空的数组之后,立刻赋值,能不能做到, System.arraycopy(source, 0, result, 0, sourceLength); //Add new data for array return result; 在返回之前,就赋一些初值. 我的意思是这样的. 现在的问题是如何赋值给一个刚创建的数组,而且要在指定的位置上.
arraycopy(Object src, int src_position, Object dst, int dst_position, int length) just so.
那,我sourceLength * 2 是不是把数组的定义扩大了2倍?
然后把2002/2003 COPY到那个位置上?
是不是这个意思?
另外arraycopy怎么用呢?
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. 明白?
还有一个问题,我能在新的数组里面赋值吗?
我是说在这里
System.arraycopy(source, 2002, result, 2003, sourceLength);
//Add new data for array
return result;
这样是不是把原来的数组也给改变了呢?
twoDimStr[?][?]
是twoDimStr[5][10]?int[] dimStr = {5, 10};
String[][] twoDimStr = (String[][]) Array.newInstance(String.class,dimStr);System.out.println("dimStr:");
for ( k = 0; k < Array.getLength(dimStr); k++)
System.out.println(dimStr[k]);System.out.println("twoDimStr:");
for ( k = 0; k < Array.getLength(twoDimStr); k++)
for ( i=0;i< Array.getLength(twoDimStr); i++)
System.out.println(twoDimStr[k][i]);
{
public static void main(String args[])
{
int[] nYear = {1998, 1999, 2000, 2001};
int[] nOtherYears = {18, 19, 20, 21};
System.arraycopy(nYear, 2, nOtherYears, 2, 2);
for (int i=0;i< Array.getLength(nOtherYears);i++ )
{
System.out.println(nOtherYears[i]+" is bissextile year");
}
}
}
//
result is :
nYear = {1998, 1999, 2000, 2001};
nOtherYears = {18, 19, 2000, 2001};
2> 是twoDimStr[5][10]?yes
我的想法是在COPY 一个新的空的数组之后,立刻赋值,能不能做到,
System.arraycopy(source, 0, result, 0, sourceLength);
//Add new data for array
return result;
在返回之前,就赋一些初值.
我的意思是这样的.
现在的问题是如何赋值给一个刚创建的数组,而且要在指定的位置上.
array2 = {0,0,0,0,0,0,0,0}
创建完成之后,要变成这样 array2 = {0,0,0,0,2002,2003,2004,2005}
能不能做到呢?
如何做?