Java 数组
数组的引用
一、输出变量数组和其值
public class hello{
public static void main(String[] args) {
int[] a1 = {1,2,3,4,5};
int[] a2 ;
a2=a1;
for(int i=0;i<a2.length;i++)
a2[i]++;
for(int i=0;i<a1.length;i++)
{
System.out.println("a1["+i+"]="+a1[i]);
}
}
}
“a1["+i+"] ” :字符串,字符串中"+i+"为 i 变量
运行结果:
a1[0]=2
a1[1]=3
a1[2]=4
a1[3]=5
a1[4]=6
二、toLowerCase与toUpperCase
public class hello{
public static void main(String[] args) {
String[] anArray ={"String One,STRING TWO"};
for(int i=0;i<anArray.length;i++)
{
System.out.println(anArray[i].toLowerCase());
}
}
}
toLowerCase :将字符串的字母转换为小写字母
toUpperCase :将字符串的字母转换为大写字母
三、数组的复制arraycopy
public class hello{
public static void main(String[] args) {
char[] copyFrom = {'d','e','c','a','f','f','e','i',
'n','a','t','e','d'};
char[] copyTo = new char[7];
System.arraycopy(copyFrom,2,copyTo,0,7);
System.out.println(new String(copyTo));
}
}
System.arraycopy(src, srcPos, dest, destPos, length);
参数:
src :源数组(要复制的数组)
srcPos :复制源数组的起始位置
dest :目标数组
destPos :目标数组的起始位置
length :需要复制的长度
四、多维数组的行和列
public class hello{
public static void main(String[] args) {
int[][] a1 = {{1,9,4},{0,2},{0,1,2,3,4}};
System.out.println("Length of array is:"+a1.length);//行
System.out.println("Length of row[0] is:"+a1[0].length);//列
System.out.println("Length of row[1] is:"+a1[1].length);
System.out.println("Length of row[2] is:"+a1[2].length);
}
}