0


第四章 数组

4.1 概念

数组是一个变量,存储相同数据类型的一组数据,java中数组是长度是固定的,下标是从0开始的,数组会自动初始。

package cn;publicclassArr1{publicstaticvoidmain(String[]  args){int[] ns =newint[3];//0 1 2
        ns[ns.length-1]=10;//异常 下标越界
        System.out.println(ns[0]);//0
        System.out.println(ns[1]);//0
        System.out.println(ns[2]);//10boolean[] fs =newboolean[3];
        System.out.println(fs[0]);//false
        
        String[] ss =newString[]{"hello","java","javascript"};
        System.out.println(ss.length);//3
        System.out.println(ss[2]);//javascript
        
        String s2[]={"aa","cc","dd","ee"};
        String[] s3 =newString[6];
        System.out.println(s3[1]);//null//二维数组double[][] ds =newdouble[][]{{10,20,30},{60,70,80},{11,22,33,44,55,66}};//System.out.println(ds[1][5]);//异常错误
        System.out.println(ds[2][5]);//66.0}}
package cn;import java.util.Date;import java.util.Scanner;publicclassArr2{publicstaticvoidmain(String[] args){// java 中 java.lang.Object 此类为所有类的父类,(基类)
        Object[] os ={1,2.0,"java",'c',true,newDate(),newScanner(System.in)};

        Scanner sc =(Scanner) os[os.length -1];int t = sc.nextInt();
        System.out.println(t);}}
  • 数组遍历package cn;import java.util.Arrays;import java.util.Random;publicclassArr3{publicstaticvoidmain(String[] args){int[] ns =newint[10]; Random rand =newRandom();for(int i=0;i<ns.length;i++) ns[i]= rand.nextInt(100)+1; System.out.println(Arrays.toString(ns));//数组遍历输出for(int i : ns){ System.out.println(i);}}}

4.2 java.util.Arrays工具类

package cn;import java.util.Arrays;publicclassArr5{publicstaticvoidmain(String[] args){// int数组 简单类型int[] nn ={10,5,6,2,1,20,15,30};
        System.out.println(Arrays.toString(nn));
        Arrays.sort(nn);// 升序
        System.out.println(Arrays.toString(nn));// Integer数组 对象类型 Integer 是int的包装器类
        Integer[] n2 ={10,5,6,2,1,20,15,30};
        System.out.println(Arrays.toString(n2));
        Arrays.sort(n2);// 升序
        System.out.println(Arrays.toString(n2));
        Arrays.sort(n2,(a, b)-> b - a);// 降序
        System.out.println(Arrays.toString(n2));int a =10;
        Integer b =20;

        Integer[] cc =newInteger[nn.length];for(int i =0; i < cc.length; i++){
            cc[i]= nn[i];}
        Arrays.sort(cc);
        System.out.println(Arrays.toString(cc));
        Arrays.sort(cc,(x, y)-> y - x);
        System.out.println(Arrays.toString(cc));}}

Student.java 类

package cn;publicclassStudent{private String sno;private String sname;privateint score;public String getSno(){return sno;}publicvoidsetSno(String sno){this.sno = sno;}public String getSname(){return sname;}publicvoidsetSname(String sname){this.sname = sname;}publicintgetScore(){return score;}publicvoidsetScore(int score){this.score = score;}publicStudent(String sno, String sname,int score){super();this.sno = sno;this.sname = sname;this.score = score;}publicStudent(){super();// TODO Auto-generated constructor stub}@Overridepublic String toString(){return"Student [sno="+ sno +", sname="+ sname +", score="+ score +"]";}}

排序,升序降序

package cn;import java.util.Arrays;publicclassArr6{publicstaticvoidmain(String[] args){
        String[] ls ={"python","c","go","perl","php","java","javascript","html","css","mysql"};
        System.out.println(Arrays.toString(ls));
        Arrays.sort(ls);// 升序
        System.out.println(Arrays.toString(ls));
        Arrays.sort(ls,(a, b)-> b.compareTo(a));// 自然排序 降序
        System.out.println(Arrays.toString(ls));

        Arrays.sort(ls,(a, b)-> a.length()- b.length());// 按字符个数,升序
        System.out.println(Arrays.toString(ls));
        Arrays.sort(ls,(a, b)-> b.length()- a.length());// 按字符个数,降序
        System.out.println(Arrays.toString(ls));

        Student[] sts =newStudent[3];
        sts[0]=newStudent("10","李四",80);
        sts[1]=newStudent("11","张三丰",30);
        sts[2]=newStudent("12","赵强",90);
        System.out.println(Arrays.toString(sts));
        Arrays.sort(sts,(a,b)->a.getScore()-b.getScore());
        System.out.println(Arrays.toString(sts));
        Arrays.sort(sts,(a,b)->b.getScore()-a.getScore());
        System.out.println(Arrays.toString(sts));}}

4.3 数组排序

面试题:声明10(1-100)随机整数的数组,遍历输出,冒泡排序,再次遍历输出。

package cn;import java.util.Arrays;import java.util.Random;publicclassArr7{publicstaticvoidmain(String[] args){int[] num =newint[10];
        System.out.println(Arrays.toString(num));// 赋值
        Random rand =newRandom();for(int i =0; i < num.length; i++)
            num[i]= rand.nextInt(100)+1;
        System.out.println(Arrays.toString(num));// 排序算法  冒泡排序,降序for(int m =0; m < num.length; m++){for(int n =0; n < num.length - m -1; n++){if(num[n]< num[n +1]){int t = num[n];
                    num[n]= num[n +1];
                    num[n +1]= t;}}}

        System.out.println(Arrays.toString(num));}}

4.4 作业

  1. 求出数组最大值,最小值
  2. 数组排序工具类Arrays使用
  3. 冒泡排序
标签: 数组 Arrays

本文转载自: https://blog.csdn.net/lianghecai52171314/article/details/123328028
版权归原作者 梁云亮 所有, 如有侵权,请联系我们删除。

“第四章 数组”的评论:

还没有评论