Home >Java >JavaBase >How to assign values ​​to arrays in java

How to assign values ​​to arrays in java

王林
王林Original
2019-11-18 17:34:1212078browse

How to assign values ​​to arrays in java

1. In array operations, you can use equal (=) assignment

Note: At this time, the new array only points to the storage space of the original array, and does not Reapply for new space.

Example:

public class ArrayTest{
	public static void main(String args[]){
		// 1
		int[] a=new int[4];
		a[0]=1;
		a[1]=2;
		a[2]=3;
		a[3]=4;
		System.out.println(a[3]);
		// 2
		int b[]=new int[4];
		b[0]=1;
		b[1]=2;
		b[2]=3;
		b[3]=4;
		System.out.println(b[2]);
		// 3
		int[] c={1,2,3,4};
		int[] d=new int[]{1,2,3,4};
		System.out.println(c[2]);
		System.out.println(d[3]);
	}
}

2. Use System.ararycopy method

System.arraycopy(originalArray, 0, targetArray, 0, originalArray.length);

Note: The new array re-applies for storage address space, and then transfers it to the original array The data is copied over.

Recommended tutorial: Java tutorial

The above is the detailed content of How to assign values ​​to arrays in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn