Java教程

Java 一维数组(超详解)

本文主要是介绍Java 一维数组(超详解),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.数组
数组的内存图?
Java的内存分配?
*堆
*栈
*方法区
本地方法栈
寄存器

	一个数组的内存图?
		int[] arr = new int[3];
		arr[0] = 12;
		arr[2] = 14;
		System.out.println(arr[0]);//12
		System.out.println(arr[1]);//0
		System.out.println(arr[2]);//14
		System.out.println(arr);//[I@7852e922

二个数组的内存图?

		int[] arr1 = new int[]{23,12,5};
		System.out.println(arr1[0]);
		System.out.println(arr1[1]);
		System.out.println(arr1[2]);
		System.out.println(arr1);
		arr1[0] = 13;
		System.out.println(arr1[0]);
		
		int[] arr2 = {14,18,15};
		System.out.println(arr2[0]);
		System.out.println(arr2[1]);
		System.out.println(arr2[2]);
		System.out.println(arr2);
		arr2[1] = 12;
		System.out.println(arr2[1]);

一个变量两个数组容器的内存图?

		int[] arr = new int[3];
		System.out.println(arr[0]);
		arr[0] = 3;
		System.out.println(arr[0]);
		System.out.println(arr);
		
		arr = new int[5];
		System.out.println(arr[0]);
		arr[0] = 5;
		System.out.println(arr[0]);
		System.out.println(arr);

两个变量指向一个数组容器的内存图?

		int[] arr1 = new int[3];
		System.out.println(arr1[0]);
		arr1[0] = 12;
		System.out.println(arr1[0]);
		System.out.println(arr1);
		
		int[] arr2 = arr1;
		System.out.println(arr2[0]);
		arr2[0] = 13;
		System.out.println(arr2[0]);
		System.out.println(arr2);
		
		System.out.println(arr1[0]);

我们在使用数组的时候经常遇到的两个错误?

	1.ArrayIndexOutOfBoundsException(数组索引越界异常)
		int[] arr = new int[3];
		System.out.println(arr[0]);
		System.out.println(arr[1]);
		System.out.println(arr[2]);
		System.out.println(arr[3]);//这一行会引发数组索引越界异常,因为数组中根本没有3索引
		
	2.NullPointerException(空指针异常)
		int[] arr = {12,14,13};
		System.out.println(arr[0]);
		System.out.println(arr[1]);
		System.out.println(arr[2]);
		
		arr = null;
		System.out.println(arr[0]);//这一行会引发空指针异常,因为arr的值为null了
		
		
		注意:null是空常量,只能赋值给引用数据类型的变量
			int[] arr = null;
			String s = null;

数组的常见操作?

	遍历?
		int[] arr = {1,2,3,4,5};
		for(int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
		
	获取最值?
		方式一:
			int[] arr = {3,5,2,1,4};
		
			int max = arr[0];//记录的是元素
			
			for(int i = 1; i < arr.length; i++) {
				if(max < arr[i]) {
					max = arr[i];
				}
			}
			
			System.out.println(max);
		
		方式二:
			int[] arr = {3,5,2,1,4};
	
			int max = 0;//记录的是索引
			
			for(int i = 1; i < arr.length; i++) {
				if(arr[max] < arr[i]) {
					max = i;
				}
			}
			
			System.out.println(arr[max]);
			
	基本查找?
		Scanner sc = new Scanner(System.in);
	
		System.out.println("请您输入一个数字:");
		int num = sc.nextInt();
		
		int[] arr = {12, 4, 13, 10, 1};
		
		boolean b = false;//false代表没找到,true代表找到了
		int index = -1;
		
		for(int i = 0; i < arr.length; i++) {
			if(arr[i] == num) {
				b = true;
				index = i;
				break;
			}
		}
		
		if(b) {
			System.out.println("找到了");
		} else {
			System.out.println("没找到");
		}
		
		System.out.println(index);

数组的反转?

		反转前:{1,2,3,4,5}
		反转后:{5,4,3,2,1}
		
		方式一:
			int[] arr = {1,2,3,4,5};
	
			//反转前:
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i] + " ");
			}
			
			System.out.println();
			
			for(int i = 0; i < arr.length/2; i++) {
				int temp = arr[i];
				arr[i] = arr[arr.length-1-i];
				arr[arr.length-1-i] = temp;
			}
			
			//反转后:
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i] + " ");
			}
		
		方式二:
			for(int i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--) {
				int temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
			
	排序?
		排序前:{4,1,5,3,2}
		排序后:{1,2,3,4,5}
		
		方式一:选择排序
			int[] arr = {4,1,5,3,2,9};
	
			//排序前:
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i] + " ");
			}
			System.out.println();
			
			for(int i = 0; i < arr.length - 1; i++) {
				for(int j = i + 1; j < arr.length; j++) {
					if(arr[i] > arr[j]) {
						int temp = arr[i];
						arr[i] = arr[j];
						arr[j] = temp;
					}
				}
			}
			
			//排序后:
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i] + " ");
			}
		
		方式二:冒泡排序
			int[] arr = {4, 1, 5, 3, 2};
	
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i] + " ");
			}
			System.out.println();
			
			for(int i = 0; i < arr.length - 1; i++) {
				for(int j = 0; j < arr.length-1-i; j++) {
					if(arr[j] > arr[j+1]) {
						int temp = arr[j];
						arr[j] = arr[j+1];
						arr[j+1] = temp;
					}
				}
			}
		
			for (int i = 0; i < arr.length; i++) {
				System.out.print(arr[i] + " ");
			}
			System.out.println();
这篇关于Java 一维数组(超详解)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!