Brief summary of java two-dimensional array

Table of contents

Array copy:

Array inversion:

Array expansion:

Code:

Code effect:

Array sorting and searching:

Applications of two-dimensional arrays:

Yang Hui Triangle:

Code:

Code effect:

Some interesting exercises:

Exercise 1:

Exercise 2:

Code:

Code effect:

 Exercise 3:

Code:

Code effect:


Array copy:

We define an array arr containing {4, 5, 6}, we also define an array arr2, the new array length = arr.length, we use the for loop to traverse arr to copy it to arr2 .

Array inversion:

Also define an array arr, use len to record the length of the array: arr.length, regardless of whether the number of elements in the array is odd or even, let len/2 get half the length of the array, then define two variables i and j , the i subscript starts from 0, and the j subscript starts from len-1. At the same time, an intermediate variable temp is introduced to facilitate the exchange of the first element of the array.

For details, see an array reverse order blog I wrote before!

Briefly describe the reverse order of java to realize the array - edsu's blog - CSDN blog

Array expansion:

We define an arr array {1,2,3,4,5}; subscript 0-4, the length of the array is 5, then we define a new array arr2, the length is arr.length +1, and then the arr array Copy the element to arr2, and directly assign the element we want to add to arr2[arr2.lenget-1]. Then let arr point to arr2, arr=arr2, and the old array of arr is destroyed. We use do- while plus a judgment statement to allow users to choose whether to expand.

Code:

import java.util.Scanner;//引入包,用户才能使用输出功能
public class Day03{
	public static void main (String[] args){
		Scanner myScanner = new Scanner(System.in);
		int arr[] = {1,2,3,4,5};
		//使用一个变量保存用户扩容的数组内容
		do{
			int arr2[] = new int [arr.length + 1];
			System.out.println("请输入你要添加的元素");
			int addNum = myScanner.nextInt();
			for(int i = 0 ; i < arr.length ; i++ ){
				arr2[i]=arr[i];
			}//是旧数组里的数组元素遍历到新数组中
			arr2[arr2.length-1] = addNum;
			arr=arr2;
			System.out.println("===扩容后的数组===");
			for(int i=0;i<arr.length;i++){
				System.out.print(arr[i]+"\t");
			}
				System.out.println("是否继续添加: y/n");
			char  key = myScanner.next().charAt(0);	
			if( key == 'n'){
	 			System.out.println("-.-");
				break;
			}	
		 }while(true);
	}
}

Code effect:

 

Array sorting and searching:

Briefly describe java's implementation of bubble sorting and sequential search - Programmer Sought

Applications of two-dimensional arrays:

Yang Hui Triangle:

We print a Yang Hui triangle with 10 rows

Law: We can see that the triangle is composed of multiple one-dimensional arrays, and the first and last elements of the one-dimensional array are both = 1.
       Other element symbols and rules: a[i][j]=a[i-1 ][j]+a[i-1][j-1];

Code:

//import java.util.Scanner;//引入包,用户才能使用输出功能
public class YangHui{
	public static void main (String[] args){
		//1
		//1 1
		//1 2 1
		//1 3 3  1
		//1 4 6  4  1
		//1 5 10 10 1
		//我们可以看出该三角型由多个一维数组组成,且一维数组第一个和最后一个元素都=1
		//其他元素符号规律:a[i][j]=a[i-1][j]+a[i-1][j-1];
		int arr[][] = new int [10][];//定义一个有10行的二维数组,一维数组具体大小不确定
		for(int i = 0 ; i < arr.length ;i++ ){//遍历二维数组里的一维数组,arr.length(多少行)
			arr[i] = new int [i+1];//给每一行的一维数组开空间
			for(int j = 0 ; j <arr[i].length ; j++){
				
				if(j==0 || j==i){//特殊条件,都赋值为1
					arr[i][j]=1;
				}
				else{
					arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
				}
			}

		}
		//打印杨辉三角
		for(int i = 0 ; i < arr.length ; i++){
			for(int j = 0 ; j < arr[i].length ; j++){
				System.out.print(arr[i][j]+"\t");
			}
			System.out.println();//换行
		}
		
	}
}

Code effect:

Some interesting exercises:

Exercise 1:

int [ ] x, y [ ] ; If the code is written like this, the array x represents a one-dimensional array, and the array y represents a two-dimensional array.

Exercise 2:

It is known that there is an ascending array {1, 3, 5, 7, 9}; after inserting elements, it can still be output in ascending order. For example, if 6 is inserted, the output array is {1, 3, 5, 6, 7, 9} ; Or insert -1, the output is {-1, 1, 3, 5, 6, 7, 9} ;

Code:

//import java.util.Scanner;//引入包,用户才能使用输出功能
public class HomeWork01{
	public static void main (String[] args){
		int arr[]={1,3,5,7,9};
		int addNum = -1;//addNum<=arr[i]
		int index= -1;//用于判断是否在数组中找到增加数的位置
		for(int i = 0 ; i < arr.length ;i++){
			if(addNum <= arr[i]){
				index=i;
				break;//找到一个就跳出循环。
			}
		}//判断addNum的位置
		if(index== -1){//增加数最大,位于数组的最后位置。
			index = arr.length;
		}
		//扩容数组
		//将原来数组拷贝到新数组
		//而且新数组的index位置不能被拷贝
		int arr2[] = new int[arr.length + 1];
		for(int i=0 ,j = 0 ; i < arr2.length ; i++){//i指向新数组,j指向原来的数组
			if(index != i){
				arr2[i] = arr[j];
				j++;//j不一定跟i同加
			}else{
				arr2[i] = addNum;
			}
		}
		arr = arr2;//将arr2赋给新数组,老的数组当垃圾
		System.out.println("===插入后的新数组情况===");
		for(int i= 0; i< arr.length ; i++){
			System.out.print(arr[i]+"\t");

		}

	}
}

Code effect:

 Exercise 3:

Randomly generate 10 integers of 0-100 and save them in the array, print them in reverse order and output their average value, find the maximum value and the largest subscript, and then judge whether there is 8 among the 10 random numbers, and give feedback.

//Generate a random integer from 1-100 ( Note : Math must be capitalized!)
//(int)(Math.random()*100 + 1)

Code:

//import java.util.Scanner;//引入包,用户才能使用输出功能
public class HomeWork03{
	public static void main (String[] args){
		//生成一个1-100的的随机整数
		//(int)(Math.random()*100 + 1)
		int arr[] = new int[10];
		int sum = 0;//求和便于计算平均值
		int len = arr.length;//保存数组长度
		int temp = arr[0];//中间变量
		int max = arr[0];//保存最大值
		int maxPose = 0;//保存最大值下标 
		int index = -1;
		System.out.print("产生十个1-100的随机数: ");
		for(int i= 0 ; i < len ; i++){
			arr[i] = (int)(Math.random()*100 + 1);
			System.out.print(arr[i] + "\t");
			if(arr[i]>max){
				max = arr[i];
				maxPose = i;
			}
			if(arr[i]==8){
				index = i;
				System.out.println("\n存在元素8");
			}
		}
		System.out.println("\n该数组中最大值" + max);
		System.out.println("\n该数组中最大的下标元素=" + maxPose);
		if(index==-1){
			System.out.println("\n该数组中没有8这个元素");
		}
		//保存十个1-100的整数到arr数组中
		//倒序输出,求平均值,求最大值和最大值的下标,并查找是否有8
		for(int i = 0,j = len-1 ; i < len/2 ; i++,j--){
			temp=arr[i];
			arr[i]=arr[j];
			arr[j]=temp;
		}//逆序数组
		//倒序输出
		System.out.print("\n打印倒序");
		for(int i= 0 ; i < len ; i++){
			System.out.print(arr[i] + "\t");
		}
		//
		
	}
}

Code effect:

Bad luck! Did not come out 8.

Guess you like

Origin blog.csdn.net/weixin_64131583/article/details/123054956
Recommended