zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然JAVA数组与方法学习笔记:数组的定义以及使用

    public class ArrayDemo01{
        public static void main(String args[]){
            int score[] = null ;            // 声明数组
            score = new int[3] ;            // 为数组开辟空间,大小为3
            System.out.println("score[0] = " + score[0]) ;
            System.out.println("score[1] = " + score[1]) ;
            System.out.println("score[2] = " + score[2]) ;
            for(int x=0;x<3;x++){
                System.out.println("score["+x+"] = " + score[x]) ;
            }
            System.out.println("score[3] = " + score[3]) ;2009-1-19
        }
    };
    public class ArrayDemo02{
        public static void main(String args[]){
            int score[] = null ;            // 声明数组
            score = new int[3] ;            // 为数组开辟空间,大小为3
            for(int x=0;x<3;x++){            // 为每一个元素赋值
                score[x] = x * 2 + 1 ;        // 每一个值都是奇数
            }
            for(int x=0;x<score.length;x++){
                System.out.println("score["+x+"] = " + score[x]) ;
            }
        }
    };
    public class ArrayDemo03{
        public static void main(String args[]){
            int score[] = null ;            // 声明数组
            score = new int[3] ;            // 为数组开辟空间,大小为3
            System.out.println("数组长度为:" + score.length) ;
        }
    };
    public class ArrayDemo04{
        public static void main(String args[]){
            int score[] = {91,92,93,94,95,96} ;        // 使用静态初始化声明数组
            for(int x=0;x<score.length;x++){        // 循环输出
                System.out.println("score["+x+"] = " + score[x]) ;
            }
        }
    };
    public class ArrayDemo05{
        public static void main(String args[]){
            int score[] = {67,89,87,69,90,100,75,90} ;        // 使用静态初始化声明数组
            int max = 0 ;                        // 保存数组中的最大值
            int min = 0 ;                        // 保存数组中的最小值
            max = min = score[0] ;                // 把第一个元素的内容赋值给max和min
            for(int x=0;x<score.length;x++){        // 循环输出
                if(score[x]>max){                // 依次判断后续元素是否比max大
                    max = score[x] ;            // 如果大,则修改max的内容
                }
                if(score[x]<min){                // 依次判断后续的元素是否比min小
                    min = score[x] ;            // 如果小,则修改min内容
                }
            }
            System.out.println("最高成绩:" + max) ;
            System.out.println("最低成绩:" + min) ;
        }
    };
    public class ArrayDemo06{
        public static void main(String args[]){
            int score[] = {67,89,87,69,90,100,75,90} ;        // 使用静态初始化声明数组
            for(int i=1;i<score.length;i++){
                for(int j=0;j<score.length;j++){
                    if(score[i]<score[j]){    // 交换位置
                        int temp = score[i] ;    // 中间变量
                        score[i] = score[j] ;
                        score[j] = temp ;
                    }
                }
            }
            for(int i=0;i<score.length;i++){    // 循环输出
                System.out.print(score[i]+"	") ;
            }
        }
    };
    public class ArrayDemo07{
        public static void main(String args[]){
            int score[] = {67,89,87,69,90,100,75,90} ;        // 使用静态初始化声明数组
            for(int i=1;i<score.length;i++){
                for(int j=0;j<score.length;j++){
                    if(score[i]<score[j]){    // 交换位置
                        int temp = score[i] ;    // 中间变量
                        score[i] = score[j] ;
                        score[j] = temp ;
                    }
                }
                System.out.print("第" + i + "次排序的结果:") ;
                for(int j=0;j<score.length;j++){    // 循环输出
                    System.out.print(score[j]+"	") ;
                }
                System.out.println("") ;
            }
            for(int i=0;i<score.length;i++){    // 循环输出
                System.out.print(score[i]+"	") ;
            }
        }
    };
    public class ArrayDemo08{
        public static void main(String args[]){
            int score[][] = new int[4][3] ;            // 声明并实例化二维数组
            score[0][1] = 30 ;                        // 为数组中的内容赋值
            score[1][0] = 31 ;                        // 为数组中的内容赋值
            score[2][2] = 32 ;                        // 为数组中的内容赋值
            score[3][1] = 33 ;                        // 为数组中的内容赋值
            score[1][1] = 30 ;                        // 为数组中的内容赋值
            for(int i=0;i<score.length;i++){
                for(int j=0;j<score[i].length;j++){
                    System.out.print(score[i][j] + "	") ;
                }
                System.out.println("") ;
            }
        }
    };
    public class ArrayDemo09{
        public static void main(String args[]){
            int score[][] = {
                {67,61},{78,89,83},{99,100,98,66,95}
            } ;        // 静态初始化完成,每行的数组元素个数不一样1        
            for(int i=0;i<score.length;i++){
                for(int j=0;j<score[i].length;j++){
                    System.out.print(score[i][j] + "	") ;
                }
                System.out.println("") ;
            }
        }
    };
    public class ArrayDemo10{
        public static void main(String args[]){
            int score[][][] = {
                {
                    {5,1} ,{6,7}
                },
                {
                    {9,4},{8,3}
                }
            } ;        // 静态初始化完成,每行的数组元素个数不一样1        
            for(int i=0;i<score.length;i++){
                for(int j=0;j<score[i].length;j++){
                    for(int k=0;k<score[i][j].length;k++)
                    {
                        System.out.println("score["+i+"]["+j+"]["+k+"] = "+score[i][j][k] + "	") ;
                    }
                }
            }
        }
    };
  • 相关阅读:
    __FILE__ php解析
    一时之悟
    apidoc生成API文档,Thinkphp6使用ThinkPHP-ApiDoc
    MySQL中的共享锁与排他锁
    Linux 挂载点目录及其作用
    IDE Eval Resetter:JetBrains 全家桶无限试用插件
    开发工具地址
    快能力和慢能力
    nginx 配置隐藏index.php效果
    Object.assign 是浅拷贝还是深拷贝
  • 原文地址:https://www.cnblogs.com/tszr/p/12435760.html
Copyright © 2011-2022 走看看