• Java基础之二维数组的回顾


    class ArrayWork {
    
        /*
        *    二维数组的复习!
        *
        *    2014年4月2日 21:45:50
        *
        *
        *
        **/
    
        public static void main(String[] args){
            
            int [] arr = {2,4,4,3,64,85,84,3,24,89,4};
    
            System.out.println("-------------------------------");
    
            //找出整型数组的是2倍数的数有几个
            int count = 0;
            for (int i = 0;i<arr.length ;i++ ){
                if (arr[i] % 2 == 0){
                    count++;
                }
            }
            System.out.println("数组中2的倍数: " + count + "个");
    
            System.out.println("-------------------------------");
    
            boolean bo = false;
            for (int i = 0;i<arr.length ;i++ ){
                if (arr[i] % 2 == 0){
                    bo = true;
                }
            }
            if (bo){
                System.out.println("存在!");
            }else{
                System.out.println("不存在!");
            }
            for (int i = 0;i<arr.length ;i++ ){
                System.out.print(arr[i]+" , ");
            }
    
            System.out.println();
            System.out.println("-------------------------------");
            int star = arr[0];
    
            for (int i = 0;i<arr.length-1;i++ ){
                arr[i]=arr[i+1];
            }
    
            arr[arr.length-1]=star;
    
            for (int i = 0;i<arr.length ;i++ ){
                System.out.print(arr[i]+" , ");
            }
    
            System.out.println();
            System.out.println("-------------------------------");
    
            boolean boo = false;
            for (int i = 0;i<arr.length ;i++ ){
                if (arr[i] % 5 == 0){
                    System.out.println(arr[i]);
                    boo = true;
                    break;
                }
            }
            if (boo==false){
                System.out.println("不存在!");
            }
    
            System.out.println("-------------------------------");
            //改变数组元素值,将一个整型数组里下标是偶数的元素都乘3
            for (int i= 0;i<arr.length ;i++ ){
                if (i%2==0){
                    arr[i]=arr[i]*3;
                }
            }
            for (int i = 0;i<arr.length ;i++){
                System.out.print(arr[i]+" , ");
            }
            System.out.println();
            
    
            System.out.println("-------------------------------");
            //改变数组元素值,将一个整型数组里下标是奇数的元素都加3
            for (int i= 0;i<arr.length ;i++ ){
                if (i%2!=0){
                    arr[i]=arr[i]+3;
                }
            }
            for (int i = 0;i<arr.length ;i++){
                System.out.print(arr[i]+" , ");
            }
            System.out.println();
    
            System.out.println("-------------------------------");
            //一个整型数组,有10个元素,打印出低于数组元素平均值的元素
            int brr []  = {2,3,4,8,6,5,4,1,32,5};
            int sun = 0;
            for (int i = 0;i<brr.length ;i++ ){
                sun += brr[i];
            }
            for (int i = 0;i<brr.length ;i++ ){
                if (brr[i] < sun / brr.length){
                    System.out.print(brr[i] + " , ");
                }
            }
            System.out.println();
    
            System.out.println("-------------------------------");
            //将数组中第一个元素移到最后数组末尾,其余数据依次往前平移一个位置。
            for (int i = 0;i<arr.length ;i++ ){
                System.out.print(arr[i]+" , ");
            }
    
            System.out.println();
            
            int star2 = arr[0];
    
            for (int i = 0;i<arr.length-1;i++ ){
                arr[i]=arr[i+1];
            }
    
            arr[arr.length-1]=star2;
    
            for (int i = 0;i<arr.length ;i++ ){
                System.out.print(arr[i]+" , ");
            }
    
            System.out.println();
    
    
            System.out.println("-------------------------------");
    
            boolean bos = false;
            for (int i = 0;i<arr.length ;i++ ){
                if (arr[i] % 2 == 0){
                    bos = true;
                }
            }
            if (bos){
                System.out.println("存在!");
            }else{
                System.out.println("不存在!");
            }
    
            System.out.println("-------------------------------");
            //一个整型数组,找出最大数所在位置
            int big = arr[0];
            int count0 = -1;
            for (int i = 0;i<arr.length ;i++ ){
                if (arr[0]<arr[i]){
                    arr[0]= arr[i];
                    count = i;
                }
            }
            System.out.println("max下标: "+ count);
    
            System.out.println("-------------------------------");
            //一个整型数组,找出最小数所在位置,并将它与第一个数对调位置
            //int crr [] = {5,4,5,6,1,3,2,4,8,85,3,4};
    
            int start = arr[0];
    
            for (int i = 0;i<arr.length ;i++ ){
                System.out.print(arr[i]+" , ");
            }
    
            System.out.println();
    
            int count1 = -1;
            
            for (int i = 0;i<arr.length ;i++ ){
                if (arr[0]>arr[i]){
                    arr[0]= arr[i];
                    count1 = i;
                }
            }
    
            arr[count1] = start;
    
            for (int i = 0;i<arr.length ;i++ ){
                System.out.print(arr[i]+" , ");
            }
    
            System.out.println();
    
            System.out.println("-------------------------------");
            //查找数组中是否有等于 5 这个元素,有则返回这个元素在数组中的位置;没有,则返回 -1
            int crr [] = {5,4,5,6,1,3,2,4,8,85,3,4};
            int vart  = -1;
            for (int i = 0;i<crr.length ;i++){
                if (crr[i]==5){
                    vart = i;
                }
            }
            System.out.println(vart);
    
            System.out.println("-------------------------------");
            //输出数组的前n的值,n是一个变量
            int suru = 200;
            for (int i = 0;i<arr.length ;i++ ){
                if (arr[i]<suru){
                    System.out.print(arr[i] + " , ");
                }
            }
            System.out.println();
            
    
            System.out.println("-------------------------------");
            //删除数组中下标值是k的元素
            int drr []={5,2,2,4,5,6};
            int sd = 3;
            for (int i = sd; i<drr.length-1;i++ ){
                drr[i] = drr[i+1];
            }
            for (int i = 0;i<drr.length-1 ;i++ ){
                System.out.print(drr[i]+" , ");
            }
            System.out.println();
            System.out.println("-------------------------------");
            //定义一个整型数组,将一个数插在数组元素的末尾形成新的数组。输出新数组
            int err [] = {5,2,2,4,5,6,324,23};
            int frr [] = new int [err.length+1];
            
            for (int i = 0;i<err.length ;i++ ){
                frr[i]= err[i];            
            }
            frr[frr.length-1] = suru;
    
            for (int i = 0;i<frr.length ;i++ ){
                System.out.print(frr[i]+" ,");
            }
            System.out.println();
            System.out.println("-------------------------------");
            //定义两个数组,将这两个数组合并形成新的数组,打印新数组
            int grr []={2,1,5,6}; 
            int hrr []={3,5,6,7,4};
            int irr []=new int [grr.length+hrr.length]; 
            for (int i = 0;i<grr.length ;i++ ){
                irr[i]=grr[i];
            }
            for (int i = 0;i<hrr.length ;i++ ){
                irr[grr.length+i]=hrr[i];
            }
            for (int i = 0;i<irr.length ;i++ ){
                System.out.print(irr[i]+" ,");
            }
    
            System.out.println();
            System.out.println("-------------------------------");
            //16.定于1001班一个数组,里面存放大家的英语成绩,
            //       (1)统计不及格人数,并打印他们的分数,
            //       (2)统计成绩在全班平均分及平均分之上的学生人数,并打印他们的分数;
            //       (3)统计各分数段的学生人数及所占的百分比。分数段(0到30,30到60,60到80,80到90,90到100)
            int jrr [] = {84,92,15,38,79,94,68,15,37,87,95,48,68,75,90,80};
            int count2 = 0;
            int count3 = 0;
            int sum = 0;
            for (int i  = 0; i<jrr.length; i++){
                sum += jrr[i];
                if (jrr[i]<60){
                    System.out.print(jrr[i]+" , ");
                    count2 ++;
                }
            }
    
            System.out.println("一共: "+ count2 +" 人");
            System.out.println("平均分: "+sum/jrr.length);
            System.out.println("下面是高于平均分的分数");
    
            for (int i = 0;i<jrr.length ;i++ ){
                if (jrr[i]>=sum/jrr.length){
                    System.out.print(jrr[i]+" , ");
                    count3++;
                }
            }
            System.out.println();
            System.out.println("高于平均分的人数有: "+ count3+" 人");
    
            double g1 = 0;//90到100
            double g2 = 0;//80到90
            double g3 = 0;//60到80
            double g4 = 0;//30到60
            double g5 = 0;//0到30
            for (int i = 0;i<jrr.length ;i++ ){
                if (90<=jrr[i] && jrr[i]<=100){
                    g1++;
                }else if (80<=jrr[i] && jrr[i]<90){
                    g2++;
                }else if (60<=jrr[i] && jrr[i]<80){
                    g3++;
                }else if (30<=jrr[i] && jrr[i]<60){
                    g4++;
                }else if (0<=jrr[i] && jrr[i]<30){
                    g5++;
                }
            }
    
            double dou1 = (g1/jrr.length)*100 ;
            double dou2 = (g2/jrr.length)*100 ;
            double dou3 = (g3/jrr.length)*100 ;
            double dou4 = (g4/jrr.length)*100 ;
            double dou5 = (g5/jrr.length)*100 ;
    
            System.out.println("90到100 : "+g1 +" 人, 占百分比: "+ dou1+ "%");
            System.out.println("90到100 : "+g2 +" 人, 占百分比: "+ dou2+ "%");
            System.out.println("90到100 : "+g3 +" 人, 占百分比: "+ dou3+ "%");
            System.out.println("90到100 : "+g4 +" 人, 占百分比: "+ dou4+ "%");
            System.out.println("90到100 : "+g5 +" 人, 占百分比: "+ dou5+ "%");
    
            System.out.println("-------------------------------");
            // 数组 
            int tab[]={10,15,20,25,30,35,40,45,50,55} ;
            int tabl = 30;
            //    设计一个程序 
            //    比如输入30,要输出4,输入10要输出0,输入55要输出9,也就是tab的下标 
            for (int i = 0;i<tab.length ;i++ ){
                if (tabl == tab[i]){
                    System.out.println(i);
                }
            }
    
            System.out.println("-------------------------------");
            //统计整型数组中的偶数个数
            int tabe[]={10,15,20,25,30,34,40,45,50,55} ;
            int count5 =0; 
            for (int i = 0; i<tabe.length; i++){
                if (tabe[i]%2==0){
                    count5++;
                }
            }
            System.out.println(count5 );
    
            System.out.println("-------------------------------");
            //输出数组中的元素 ,每3个元素一行
            int taba[]={10,15,20,25,30,34,40,45,50,55} ;
            int count6 =0; 
            for (int i = 0; i<taba.length; i++){
                if (count6 % 3 == 0){
                    System.out.println();
                }
                System.out.print(taba[i]+" , ");
                count6++;
            }
            System.out.println();
    
            System.out.println("-------------------------------");
            //计算整型数组a中的最大值及其所在的下标。
            int tabs[]={10,15,20,25,55,30,34,40,45,50} ;
            int count7 =tabs[0]; 
            int count8 = -1;
            for (int i = 0; i<tabs.length; i++){
                if (count7 < tabs[i]){
                    count7 = tabs[i];
                    count8 = i;
                }
            }
            System.out.println("最大值: "+count7 + "   下标: "+count8);
    
    
            System.out.println("-------------------------------");
            //定义一个字符串数组,把数组中的每个元素相加后输出
            String dasf [] = {"h","e","l","l","o","w","o","r","d"};
            for (int i = 0;i<dasf.length ;i++ ){
                System.out.print(dasf[i]);
            }
            System.out.println();
    
            System.out.println("-------------------------------");
            //定义一个字符串数组,将其倒序输出
            String hello [] = {"h","e","l","l","o","w","o","r","d"};
            for (int i = hello.length-1;i>=0 ;i-- ){
                System.out.print(hello[i]);
            }
            System.out.println();
            System.out.println("-------------------------------");
            //有一个字符串数组 ,将数组中每个元素倒转,如 var x=new Array("a","b","c","d") 倒
            //    转后x数组变成了("d","c","b","a")
            String hello1 [] = {"h","e","l","l","o","w","o","r","d"};
            String hello2 [] = new String[hello1.length];
            int jsss = 0;
            for (int i = hello.length-1;i>=0 ;i-- ){
                hello2[jsss] = hello1[i];
                jsss++;
            }
            for (int i = 0;i<hello2.length ;i++ ){
                System.out.print(hello2[i]);
            }
    
            System.out.println();
            System.out.println("-------------------------------");
            //有一个字符串数组,分别统计其相同元素的个数并输出 如数组
                //("a","b","a","d","b","b")  则输出a 2个  b 3个 d 一个(本题属于高难度题目)
            String string []= {"a","b","a","d","b","b"};
            int coun1 = 0;
            int coun2 = 0;
            int coun3 = 0;
            for (int i = 0;i<string.length ;i++ ){
                if (string[i].equals("a")){
                    coun1 ++;
                }else if (string[i].equals("b")){
                    coun2 ++;
                }else if (string[i].equals("d")){
                    coun3 ++;
                }
            }    
            System.out.println("a: "+ coun1 + "    b: "+coun2 + "    d: "+coun3);
    
    
            System.out.println("-------------------------------");
            //有两个长度相同的字符串数组,判断这两个数组中的对应元素是否完全相同,如果完全相
            //    同则输出"呵呵,我爱死你了,怎么搞得跟我一样",如果有不相同
            //       则把那些不相同的元素打印出来 如("a","b","c")和("a","b","d")  则 打印 第3个元素
            //    不相同  c与d
            String str1 []= {"a","b","a","d","b","b"};
            String str2 []= {"a","b","a","2","b","b"};
            int co1 = 0;
            for (int i = 0;i<str1.length ;i++ ){
                if (str1[i].equals(str2[i])){
                    co1++;
                }else {
                    System.out.println("第"+ (i+1) +"个元素不相同"+str1[i]+"与"+str2[i]);
                    break;
                }
            }
            if (co1 == str1.length){
                System.out.println("呵呵,我爱死你了,怎么搞得跟我一样");
            }
    
            System.out.println("-------------------------------");
        //    两个字符串数组合并,重复的元素去掉,如("a","b","c")和("a","b","d")合并后的数组
        //        为("a","b","c","d")(高难度)
            String st1 []= {"a","b","c"}; 
            String st2 []= {"a","b","d"}; 
            String st3 []= new String [4]; 
            for (int i = 0;i<st1.length ;i++ ){
                st3[i] = st1[i];
            }
            for (int i = 0;i<st2.length ;i++ ){
                if (st3[i].equals(st2[i])){
    
                }else {
                    st3[st1.length] = st2[i];
                }
            }
    
            for (int i = 0;i<st3.length ;i++ ){
                System.out.print(st3[i]+" , ");
            }
            System.out.println();
    
            System.out.println("-------------------------------");
            
            int Int[]={15,20,25,55,30,34,10,40,45,50} ;
            int max_1=Int[0];
            int min_1=Int[0];
            for (int i = 0;i<Int.length ;i++ ){
                if (max_1<Int[i]){
                    max_1= Int[i];
                }
                if (min_1>Int[i]){
                    min_1=Int[i];
                }
            }
            System.out.println("sum:" + (min_1 + max_1));
    
            System.out.println("-------------------------------");
    
            //有一个整型数组,对数组元素进行排序,从大到小输出 (高难度,属于后面要讲的数组排
            //序内容【(比较交换法、选择法、冒泡法)】,能用你们的方法做出来固然好,做不   出来
            //别把脑想坏了)
            int Intger[]={15,20,25,55,30,34,10,40,45,50} ;
            int ssa = Intger[0];
            for (int i = 0;i<Intger.length ;i++ ){
                for (int j = 0;j<Intger.length ;j++ ){
                    if (Intger[i]<Intger[j]){
                        ssa = Intger[j];
                        Intger[j] = Intger[i];
                        Intger[i] = ssa;
                    }
                }
            }
            for (int i = 0;i<Intger.length ;i++ ){
                System.out.print(Intger[i]+",");
            }
    
            System.out.println();
            System.out.println("-------------------------------");
            int fblx [] = new int[20];
            fblx[0] = 1;
            fblx[1] = 1;
            for (int i = 2;i<fblx.length ;i++ ){
                fblx[i]=fblx[i-1]+fblx[i-2];
            }
            
            for (int i = 0;i<fblx.length ;i++ ){
                System.out.print(fblx[i]+" ,");
            }
            System.out.println();
            System.out.println("-------------------------------");
            //给定一个数据元素,要求根据这个元素把数组里的该元素删除  如var x="a";有数组 var  
                //cc=new Array("a","b","c"),则删除元素a后数组变成了("b","c")(有一定难度)
            String []cc={"a","b","c"};
            String []cc2=new String [cc.length-1];
            int catr = 0;
            for (int i = 0;i<cc.length ;i++ ){
                if (cc[i].equals("a")){
                    
                }
            }
            for (int i= catr;i<cc.length-1 ;i++ ){
                cc[i]=cc[i+1];
            }
            for (int i = 0;i<cc2.length ;i++ ){
                cc2[i]=cc[i];
            }
            for (int i = 0;i<cc2.length ;i++ ){
                System.out.print(cc2[i]+" ,");
            }
            System.out.println();
            System.out.println("-------------------------------");
            //将10个数放入到数组,然后再进行从小到大排序后输出。 
    
            int Intge[]={15,20,25,55,30,34,10,40,45,50} ;
            int sa = Intge[0];
            for (int i = 0;i<Intge.length ;i++ ){
                for (int j = 0;j<Intge.length ;j++ ){
                    if (Intge[i]<Intge[j]){
                        sa = Intge[j];
                        Intge[j] = Intge[i];
                        Intge[i] = sa;
                    }
                }
            }
            for (int i = 0;i<Intge.length ;i++ ){
                System.out.print(Intge[i]+",");
            }
    
            System.out.println();
            System.out.println("-------------------------------");
    
            //将一个数组逆序输出。有20个数字
    
            int Intg[]={15,20,25,55,30,34,10,40,45,50,15,20,25,55,30,34,10,40,45,50} ;
            int seee = Intg[0];
            for (int i = 0;i<Intg.length ;i++ ){
                for (int j = 0;j<Intg.length ;j++ ){
                    if (Intg[i]>Intg[j]){
                        seee = Intg[j];
                        Intg[j] = Intg[i];
                        Intg[i] = seee;
                    }
                }
            }
            for (int i = 0;i<Intg.length ;i++ ){
                System.out.print(Intg[i]+",");
            }
    
            System.out.println();
    
            System.out.println("-------------------------------");
            //通过输入框输入数组,提示请输入第x个数字,当输入的-1为结束。最后输出这个数组的数据。
            int Inttr[]={15,20,25,55,30,34,10,40,45,50,15,20,25,55,30,34,10,40,45,50} ;
            int sdff = -1;
            for (int i = 0;i<Inttr.length ;i++ ){
                if (i==sdff){
                    System.out.println(Inttr[i]);
                }else if (i==-1){
                    System.out.println("结束");
                    break;
                }
    
            }
            for (int i = 0;i<Inttr.length ;i++ ){
                System.out.print(Inttr[i]+" ,");
            }
            System.out.println();
            System.out.println("-------------------------------");
                //有一个数组a[2,5,9,2,6,3,1,2,5,6,7,8,4,0],统计这个数字不相同的数字有多少个,它
                //们相同的个数是多少。
            int atery [] = {2,5,9,2,6,3,1,2,5,6,7,8,4,0};
            int xt = 0;
            int bt = 0;
            for (int i = 0;i<atery.length ;i++ ){
                for (int j = 0;j<atery.length ;j++){
                    if (atery[i]==atery[j]){
                        xt++;
                    }else {
                        bt++;
                    }
                }
                
            }
            System.out.println("相同: "+xt/2+"不同: "+bt/12);
    
            System.out.println("-------------------------------");
                //有两个数组a[1,2,6,4,3,7,8,4,5,7],b[12,45,1,2,5,2,6,8,9],同时存在这两个数据的数
                //字有哪些,请输出。
            int szji1 [] = {1,2,6,4,3,7,8,4,5,7};
            int szji2 [] = {12,45,1,2,5,2,6,8,9};
            for (int i = 0;i<szji1.length ;i++ ){
                for (int j = 0;j<szji2.length ;j++){
                    if (szji1[i]==szji2[j]){
                        System.out.println(szji1[i]+"和"+szji2[j]);
                    }
                }
                
            }
            System.out.println();
            System.out.println("-------------------------------");
            //请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续    判断
            //第二个字母。 
            String day = "二";
            if (day=="一"){
                System.out.println("星期一");
            }else if (day == "二"){
                System.out.println("星期二");
            }else if (day == "三"){
                System.out.println("星期三");
            }else if (day == "四"){
                System.out.println("星期四");
            }else if (day == "五"){
                System.out.println("星期五");
            }else if (day == "六"){
                System.out.println("星期六");
            }else if (day == "日"){
                System.out.println("星期日");
            }
            System.out.println("-------------------------------");
            //.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个     
                //第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下     
                //的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
            System.out.println(fun(1));
        }
        static int fun (int daiy){
            if (daiy  == 10){
                return 1;
            }else{
                return (fun(daiy+1)+1)*2;
            }
        }
    }
  • 相关阅读:
    Tomcat容器,Servlet容器,Spring容器的包含关系
    数据库字段类型导致的前端JSON转换错误
    全局变量和局部变量
    数据库中的timestamp类型数据的存储
    layer的confirm
    网间互联协议
    局域网协议
    局域网络设备
    网络传输介质
    Docker常用命令
  • 原文地址:https://www.cnblogs.com/Toolo/p/3641678.html
Copyright © 2020-2023  润新知