zoukankan      html  css  js  c++  java
  • JAVA 基础3-数组

    一.数组的概念

      数组可以看成是多个数据类型的集合,是对这些数据进行统一的管理;

      数组的变量是引用类型,数组本身是对象,数组中的每个元素相当于该对象的成员变量;

      数组的元素可以是任何数据类型,包括基础数据类型和引用类型;

    二.数组的声明方式

      数据类型 + [ ] + 数组名称 = new + 数据类型 + [数组的长度];

      例:int[] arr = new int[6]; 也可写成int arr[] = new int[6]; 一般用第一种写法;

        也可直接定义数组中的内容:int[] arr ={1,2,3,4,5,6};注意:声明数组时必须用new关键字;

    注意:1.声明数组时要使用new关键字

       2.必须声明数组的长度;且一旦定义则长度不可改变;

       3.引用类型的数组是指在栈上声明一个数组变量,内容则存在堆上,由该变量指向该内容;

    三.数组的遍历和初始化

      1.静态初始化指直接定义该数组内容,例:int[] arr = {1,2,3,4};

      2.动态初始化是指用遍历定义该数组内容;

    四.public static void main(String[] args)

    意思为公共的 静态的 无返回值 主方法 前面3个都是用来修饰主方法的,括号里Sring是一个类,用于创建字符串对象,这里用来修饰主方法的形参,[ ]表示主方法的形参是一个字符串数组,args是一个标识符,是形参数组名。

    五.关于局部变量和成员变量的区别:

    1.在类中的位置不同:成员变量在类中方法外。局部变量在方法定义中或在方法定义上。

    2.在内存的位置不同:成员变量在堆内存。局部变量在栈内存。

    3.生命周期不同:成员变量随着对象的创建而存在,随着对象的消失而消失。 局部变量随着方法的调用而存在,随着方法的调用完毕而消失。

    4.初始化值不同:成员变量有默认值初始化。局部变量没有默认值初始化,必须定义,赋值,然后才能使用。

    5.局部变量名称可以和成员变量名称一样,在方法中使用的时候,采用的是就近原则。

    main 方法是指该程序的主入口;

    用args做运算器:

    public class Test3 {
    	public static void main(String[] args) {
    		if (args.length != 3) {
    			System.out.println("请输入正确的格式!");
    			return;
    		}
    		int a = Integer.parseInt(args[0]);
    		int b = Integer.parseInt(args[2]);
    		String s = args[1];
    		switch (s) {
    		case "+":
    			System.out.println(a + b);
    			break;
    		case "-":
    			System.out.println(a - b);
    			break;
    		case "/":
    			System.out.println(a / b);
    			break;
    		case "X":
    			System.out.println(a * b);
    			break;
    		default:
    			break;
    		}
    	}	
    }
    

     在该文件夹下按shift+鼠标右键弹出的菜单中选择命令窗口项运行;注意*在命令框中需改为大写X才行;

    例题:500逢3退1

    public class Test3 {
    	public static void main(String[] args) {
    		boolean[] person = new boolean[500];
    		for (int i = 0; i < person.length; i++) {
    			person[i] = true;
    		}
    		int index = 0;
    		int count = 0;
    		int yu = person.length;
    		while (yu != 1) {
    			if (person[index]) {
    				count++;
    				if (count == 3) {
    					count = 0;
    					yu--;
    					person[index] = false;
    				}
    			}
    			index++;
    			if (index == person.length) {
    				index = 0;
    			}
    		}
    		for (int i = 0; i < person.length; i++) {
    			if (person[i] == true) {
    				System.out.println(i);
    			}
    		}
    	}	
    }
    

     结果为435;

    思路:先让所有人定义为布尔变量true;从这个人是true时开始数,当数到3时让该人变为false;同时从0开始重新数,剩余人数少一个,当数到500时从新开始,知道剩余人数为1时停止;再用遍历输出为true的序号。

    数组排序

    public class Test4 {
    
    	public static void main(String[] args) {
    		int[] arr = { 1, 3, 6, 2, 0, 11, 17, 14, 4 };
    		bubbleSort(arr);    //冒泡排序
    	        selectSort(arr);   //选择排序
    		reverseSort(arr);  //反转排序
    		p(arr);
    	}
    	public static void reverseSort(int[] arr) {
    		int t = arr.length / 2;
    		for (int i = 0; i < t; i++) {
    			int temp = arr[i];
    			arr[i] = arr[arr.length - 1 - i];
    			arr[arr.length - 1 - i] = temp;
    		}
    	}
    	public static void selectSort(int[] arr) {                   //从大到小排序
    		for (int i = 0; i < arr.length - 1; i++) {
    			int index = i;                          //定义变量为第一个数的索引值
    			for (int j = i + 1; j < arr.length; j++) {         //如果后面的值比这个变量大,将这个值的索引赋值给变量
    				if (arr[index] < arr[j]) {
    					index = j;
    				}                             //循环结束后得到所有数中最大的数
    			}
    			int temp = arr[index];                     //将最大的数与原数位置互换,依次排序(int temp = arr[i]也可以)
    			arr[index] = arr[i];
    			arr[i] = temp;
    		}	
    	}
    //选择排序的运行思路:外循环先取第一个数,内循环让该数依次与后面的每个数比较,内循环结束后得到最大的那个数的索引值,
    外循环让最大的数与第一个数位置互换,这样最大的数排在第一位,然后取第二数重复循环。 public static void bubbleSort(int[] arr) {                  //从小到大排序 for (int i = 0; i < arr.length -1; 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; } } } }
    //冒泡排序的运行思路:外循环是指需要比较的轮数,就是让每个数都要与其他数比一次,内循环是让该数与其他数比较后,通过位置互换将最大的数排到最后面,下轮循环则不需要让其他数与该数比较,所以次数减1. public static void p(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }

     六.数组常用方法

    例 :int[] arr1 = {6, 5 4, 3, 2, 1, 0}; int[] arr2 = {6, 5, 4, 3, 2, 1, 0};

    1. 数组排序:Arrays.sort(arr); 从小到大排序,没有降序方法,可以使用循环倒序输出;
    2. 填充数组: Arrays.fill(arr, 5);  填充数组,一般用于数组的初始化,返回值都是5.
    3. 数组的复制 :Arrays.copyOf(arr, 5);如果新数组的长度不如被复制的数组长度长则有多少取多少;如果新数组的长度比较长, 不足的部分用0填充
    4. 数组的范围复制 Arrays.copyOfRange(数组名,开始索引,结束索引);索引的范围, 包括开始, 不包括结束
    5. 比较两个数组是不是一样 Arrays.equals(arr,arr2);返回值是布尔值

  • 相关阅读:
    AtCoder Beginner Contest 167
    AtCoder Beginner Contest 166
    AtCoder Beginner Contest 165
    AtCoder Beginner Contest 164
    AtCoder Beginner Contest 163
    AtCoder Beginner Contest 162
    AtCoder Beginner Contest 161
    AtCoder Beginner Contest 160
    AtCoder Beginner Contest 159
    自定义Mybatis自动生成代码规则
  • 原文地址:https://www.cnblogs.com/wyc1991/p/8965277.html
Copyright © 2011-2022 走看看