Java学习记录——数组

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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值