Some characteristics of arrays

Article directory

1, Parameters of main method

![image][main]

2, The value passing mechanism of parameters

  1. Basic data type

New ParameterDemo1.java

//Value Passing Mechanism of method parameters - basic data type
class ParameterDemo1 
{	
	static void change(int x){
		System.out.println(x);
		x = 50;
		System.out.println(x);
	}
	public static void main(String[] args) 
	{
		int x = 10;
		System.out.println(x);
		change(x);
		System.out.println(x);
	}
}

![image][deliver]

![image][deliver1]

  1. Reference data type

New ParameterDemo2.java

class ParameterDemo2 
{
	//Printing
	static void printArr(int[] arr){
		String ret = "[";
		for (int i = 0;i < arr.length ;i ++ )
		{
			ret +=arr[i];
			if (i < arr.length -1)
			{
				ret += ",";
			}
		}
		ret += "]";
		System.out.println(ret);
	} 

	//Exchange the first and last elements
	static void swap(int[] arr){
		int temp = arr[0];
		arr[0] = arr[arr.length - 1];
		arr[arr.length - 1] = temp;
	}

	public static void main(String[] args) 
	{
		int[] arr = new int[]{10,99};
		printArr(arr);

		swap(arr);
		printArr(arr);
	}
}

3, Multidimensional array

  • Strictly speaking, there is no concept of multi-dimensional array in Java, which is distinguished from C language. It is generally called array in array.
  • One dimensional array: each element in the array is a value (values of base class type and reference type).
  • Two dimensional array: each element in the array is a one-dimensional array.
  • 3D array: each element in the array is a 2D array.

Syntax:

int[] [] arr = new int[] [] {
{1,3,5},
{3,5,1,3},
{7,23,46,76,3}
};

4, Initialization of 2D array

Static initialization:

int[][] arr = new int[][]{
			{1,2,3},
			{4,5},
			{6},
		};

Dynamic initialization:

int[][] arr = new int[3][5];//Create a two-dimensional array of length 3.

int[][] arr = new int[][5];//Error, no length declared.

Generally, N-dimensional arrays need N loops to traverse, and for is preferred

//Traversal 2D array
class ArrayInArrayDemo 
{
		public static void main(String[] args) 
	{
		//Define 2D array
		int[][] arr = {
			{1,2,3},
			{4,5,6,7},
			{8,9,10}
		};
		//Traversal two-dimensional array length
		for (int i = 0;i < arr.length;i ++)
		{
			//Traversal length of one-dimensional array
			for (int j = 0;j < arr[i].length;j ++)
			{
				System.out.println(arr[i][j]);
			}
		}
	}
}

5, Java 5 support for arrays, enhance for loop

I don't care about index, but more about array elements.

For (array element type variable: array name){
Circulatory body
}

New ForEachDemo.java

//Enhance for loop foreach
class ForEachDemo 
{
	public static void main(String[] args) 
	{
		int[] arr = new int[]{2,3,5,6,7,8};
		for(int i : arr)
		{
			System.out.print(i + " ");
		}
		System.out.println();
		
		//Define 2D array
		int[][] arr = {
			{1,2,3},
			{4,5,6,7},
			{8,9,10}
		};
		
		System.out.println("-----------");
		//foreach traversal
		for (int[] i : arr)
		{
			for (int ele : i)
			{
				System.out.println(ele);
			}
		}
	}
}

The compiled bytecode is obtained by decompiling the software.

6, Java 5 - variable parameters of methods

A method can only have one variable parameter. The variable parameter must be in the back, and cannot be overloaded!

New VarArgsDemo.java

//Variable parameters of method
class VarArgsDemo 
{
	//Sum of arrays
	static double getSum(double[] arr){
		double sum = 0.0;
		for (double i : arr )
		{
			sum += i;
		}
		return sum;
	}

	//Find the sum of the array, use java5's method parameter variability, the variable parameter must follow
	static double getCount(double cutoff,double ... arr){
		double sum = 0.0;
		for (double i : arr )
		{
			sum += i;
		}
		return sum * cutoff;
	}

	public static void main(String[] args) 
	{
		double[] arr = new double[]{10.0,20.0,30.0};
		System.out.println(getSum(arr));
		System.out.println("-----------");
		//System.out.println(getCount(arr));
		System.out.println(getCount(10.0,20.0,30.0));
		System.out.println(getCount(10.0,20.0,30.0,40.0));
	}
}

Published 24 original articles, praised 0, visited 94
Private letter follow

Tags: Java C

Posted on Fri, 13 Mar 2020 07:05:38 -0400 by johnpaine