2017.08.01呀灭蝶(数组相关)

一轮明月半空中

今夜无雨

______________________________________那么,开始上课吧....应该到数组了,望同好订正___________

java的数据类型分为两大部分:基本数据类型  和   引用数据类型(数组..类..接口.)

引用传递的就是一个内存的使用权,一块内存空间,可能有多个人同时使用

数组:是一组相关变量的集合.

int i[100] 是伪代码,只是表示下含义

public class Test17{
    public static void main(String args[]){
        int score[] = null;        //声明数组
        score = new int[3];        //为数组开辟空间,大小为3
    }
}

数组元素通过"数组名称[下标]"的形式保存.

例:public class Test18{
    public static void main(String args[]){
        int score[] = new int[4];
        System.out.println("数组的长度为: " + score.length);
    }
}

230724_DEIx_3607205.png

例:

public class Test19{
    public static void main(String args[]){
        int score[] = {422,32,532,6,223,42,25,525};
        for(int i=0;i<score.length;i++){
            System.out.println("score[" + i + "] = " + score[i]);
        }
    }
}

..............求数组中的最大值和最小值

public class Test20{
    public static void main(String args[]){
        int score[] = {2342,231,1231,55123,32131,513131,123213,32145213,23131334};
        int max =0;
        int min =0;
        max = min = score[0];
        for(int i=0;i<score.length;i++){
            if(score[i]>max){
                max = score[i];
            }
            if(score[i]<min){
                min = score[i];
            }
        }
        System.out.println("数组中的最大值为: " + max + ";最小值为: " + min );
    }
}

_______给数组排序,之冒泡排序

public class Test21{
    public static void main(String args[]){
        int score[] = {7,6,8,2,5,1,3,4,9,0};//定义一个数组为其静态赋值
        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.println(score[i]);
        }
        
    }
}

二维数组:

动态赋值: int score[][]=new int[2][5];

静态赋值: int score[][]={{2,4},{1,32,12},{4,2,5},{12,44,2}};

二维数组的输出:

public class Test22{
    public static void main(String args[]){
        int score[][]={{2,4},{1,32,12},{4,2,5},{12,44,2}};
        for(int i=0;i<score.length;i++){
            for(int j=0;j<score[i].length;j++){
                System.out.print(score[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

______________________________________________嗯,结束了...晚安.___________________

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3607205/blog/1499200

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值