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

How to assign values ​​to arrays in java

王林
Release: 2019-11-18 17:34:12
Original
12060 people have browsed it

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]);
	}
}
Copy after login

2. Use System.ararycopy method

System.arraycopy(originalArray, 0, targetArray, 0, originalArray.length);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template