java数组练习

数组操作
  • 输出26个大小写字母
public class Demo04 {
    public static void main(String[] args) {
        char[] ch = new char[26];
        int foot = 0 ;
        for (int i = 97; i <= 122; i++) {
            ch[foot++] = (char) i ;
        }
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i] +"("+(char) (ch[i]-32)+")");
            //去掉最后一个顿号
            if (i != ch.length - 1) {
                System.out.print("、");
            }
        }
    }
}
运行结果:
a(A)b(B)c(C)d(D)e(E)f(F)g(G)h(H)i(I)j(J)k(K)l(L)m(M)n(N)o(O)p(P)q(Q)r(R)s(S)t(T)u(U)v(V)w(W)x(X)y(Y)z(Z)
foreach输出
public class Demo05 {
    public static void main(String[] args) {
        int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        for (int temp : array) { // temp代表数组中的每一个元素
            System.out.print(temp+"、");
        }
    }
}
二维数组的定义和使用

二维数组前面的中括号代表的是行,后面的中括号代表的是列,此时下面的代码表示的是一个4行5列的一个表格,外层循环控制行,内层循环控制列

public class Demo06 {
    public static void main(String[] args) {
        int[][] array = new int[4][5];
        for (int i = 0; i < array.length; i++) {//外层循环控制行
            for (int j = 0; j < array[i].length; j++) {//内层循环控制列,array[i].length代表列的长度
                array[i][j] = i+j ;
            }
        }
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] +" ");
            }
            System.out.println();
        }
    }
}
运行结果:
0 1 2 3 4 
1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
数组的排序
public class Demo08 {
    public static void main(String[] args) {
        int[] array = new int[]{9, 5, 3, 7, 4, 6, 1, 2, 8, 0};
        sort(array);
        print(array);
    }

    public static void sort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    public static void print(int[] arr) {
        for (int temp : arr) {
            System.out.print(temp + " ");
        }
    }
}
数组反转

1.创建一个新的数组,逆序保存后改变引用

/*
 * 数组的反转
 * */
public class Demo09 {
    public static void main(String[] args) {
        int[] array = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] data = reverse(array);
        print(data);
    }

    public static int[] reverse(int[] array) {
        int[] nums = new int[array.length];
        //获取最后一个元素的索引
        int lastIndex = array.length - 1 ;
        for (int i = 0; i < array.length; i++) {
            nums[i] = array[lastIndex--];
        }
        return nums;
    }

    public static void print(int[] array) {
        for (int temp : array) {
            System.out.print(temp+" ");
        }
    }
}

转置处理实现数组的反转
/*
* 实现数组的转置处理
* */
public class Demo10 {
    public static void main(String[] args) {
        int[] array = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        reverse(array);
        print(array);
    }

    public static void reverse(int[] array) {
        // 循环的次数
        int count = array.length/2 ;
        //头部索引
        int head = 0 ;
        //尾部索引
        int tail = array.length-1 ;
        for (int i = 0; i < count; i++) {
            int temp = array[head];
            array[head] = array[tail];
            array[tail] = temp ;
            head++ ;
            tail--;
        }
    }

    public static void print(int[] array) {
        for (int temp : array) {
            System.out.print( temp +" ");
        }
    }
}

方法可变参数

在内部处理的时候将所有传递的数据自动包装在数组里面,这样就可以传递任意多个参数了。

public class Demo12 {
    public static void main(String[] args) {
        System.out.println(print(new int[]{1,2,3}));
        System.out.println(print(1,2,3,4,5));
    }

    public static int print(int... array) {
        int sum = 0 ;
        for (int arr : array) {
            sum+=arr;
        }
        return sum ;
    }
}

对象数组
class Call{
    private String classify ;
    private double price;

    public Call(String classify , double price) {
        this.classify = classify ;
        this.price = price ;
    }

    public String getInfo() {
        return "球类型:"+this.classify +" , 球价格:"+this.price;
    }
}
public class Demo13 {
    public static void main(String[] args) {
        Call[] call = new Call[3];
        call[0] = new Call("篮球", 19.9);
        call[1] = new Call("足球", 29.9);
        call[2] = new Call("排球", 39.9);
        for (Call temp : call) {
            System.out.println(temp.getInfo());
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值