zoukankan      html  css  js  c++  java
  • 4.27数组

    概念:
      数组可以看成是多个相同类型数据的集合, 对这些数据的统一管理
      数组的变量是引用类型, 数组也本身也是个对象,
      数组中的每个元素相当于该对象的成员变量
      数组的元素可以是任何的数据类型, 包括基本数据类型和引用类型

    声明方式:
      类型[ ] 变量名 = new 类型[长度]    一般写这种
      或者
      类型 变量名[ ] = new 类型[长度]
      int[ ] a = new int [10];
      double[ ] d;
      String[ ] s;
      Person[ ] p;

    声明数组时要使用 new 关键字,           new表示在空间上新开辟一个空间用来存放变量
      1, 必须声明数组的长度, int[ ] a = new int [5];
      2, 也可以直接定义数组的内容 int[ ] a = { 1, 2, 3 }
      3, 数组的长度一旦被定义, 则不可再变

    遍历和初始化(求int数组的和)
           ① 静态初始化 / 动态初始化         静态是定义完数组以后直接给值,动态是先定义长度

                    例: int[ ] arr = new int[10]; 

                              int[ ] arr2;    只声明了变量,什么也不指向

            double[ ] d = new  double[10];

                           int[ ] arr3 = {4,3,1};
         ②  基础类型
            ③ 引用类型
         ④ 二维数组的初始化和定义: int[ ][ ] aa = new int[3][ ];

    练习题:500人,第三个淘汰

    package test;
    
    public class Count3Quit {
    
    	public static void main(String[] args) {
    		boolean[] persons = new boolean[500];      定义500人的数组
    		for (int i = 0; i < persons.length; i++) {
    			persons[i] = true;                                  一开始500人都在队伍    
    		}
    		
    		int index = 0;                          // 记录数到整个队伍中的第几个
    		int count = 0; // 123,123,123
    		int len = persons.length;                 // 队伍中还剩下多少人
    		
    		while (len != 1) {                            只要不是剩一个人就一直循环
    			if (persons[index]) {
    				count++;
    				if (count == 3) {              数到第三个的时候
    					persons[index] = false;           踢出去
    					count = 0;                     
    					len--;            
    				}
    			} 
    			index++;                            如果等于队伍的长度的话变0重新数
    			if (index == persons.length) {
    				index = 0;
    			}
    		}
    		                                                                                 
    		for (int i = 0; i < persons.length; i++) {
    			if (persons[i]) {                                      
    				System.out.println(i);
    			}
    		}
    		
    	}
    
    }
    

    冒泡排序 :

    public class Test4 {
    
    	public static void main(String[] args) {
    		int[] arr = { 1, 3, 6, 2, 0, 11, 17, 14, 4 };
    		 bubbleSort(arr);		
    		 p(arr);
    				
    	public static void bubbleSort(int[] arr) {
    		for (int i = 0; i < arr.length; i++) {
    			for (int j = 0; j < arr.length - i - 1; j++) {
    				if (arr[j] > arr[j + 1]) {
    					int temp = arr[j];
    					arr[j] = arr[j + 1];
    					arr[j + 1] = temp;
    				}
    			}
    		}
    	}
    	public static void p(int[] arr) {
    		for (int i = 0; i < arr.length; i++) {
    			System.out.print(arr[i] + " ");
    		}
    	}
    }
    

      

     

  • 相关阅读:
    Centos6.5系统压力测试过程大量TIME_WAIT
    几种常用的数据库连接池
    weblogic弱密码检测
    ubuntu系统查看已安装的软件
    Flask Web中用MySQL代替SQLite
    SQLALCHEMY_TRACK_MODIFICATIONS adds significant异常的解决方法
    安装ipython时python setup.py egg_info错误的解决办法
    python manage.py runserver指定端口和ip
    Python连接mysql出错,_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
    linux重启服务的脚本命令
  • 原文地址:https://www.cnblogs.com/syx1997/p/8963379.html
Copyright © 2011-2022 走看看