Understanding the initialization of the java-4.8 array from the beginning (1)

Source: Internet
Author: User
Tags array length throw exception

In this chapter we will discuss the initialization of the array.

1. An array is an object.

Package Com.ray.ch01;public class Test {public static void main (string[] args) {int[] a = {}; System.out.println (a instanceof Object);}}

Output:

True


2. Assigning an array to a variable is actually assigning a reference to the array to the variable.

Package Com.ray.ch04;import Java.util.arrays;public class Test {public static void main (string[] args) {int[] A1 = {1, 2, 3, 4, 5};int[] a2;a2 = A1; System.out.println (a2.equals (A1)); for (int i = 0; i < a2.length; i++) {A2[i] + = 1;} System.out.println (arrays.tostring (A1));}}

Output:

True
[2, 3, 4, 5, 6]

From the above results can be seen, A1, A2 point to the same object, when changing A2, A1 also change.


Arrays can be placed not only on the underlying type, but also on objects.

Package Com.ray.ch04;public class Test {public static void main (string[] args) {integer[] a = {new Integer (1), New intege R (2)};}}



3. Throw exception when read length exceeds array length

Change the code above

Package Com.ray.ch04;import Java.util.arrays;public class Test {public static void main (string[] args) {int[] A1 = {1, 2, 3, 4, 5};int[] a2;a2 = A1; System.out.println (a2.equals (A1)); for (int i = 0; I <= a2.length; i++) {//change here, one more = number A2[i] + = 1;} System.out.println (arrays.tostring (A1));}}

Output:

True
Exception in thread "main" Java.lang.arrayindexoutofboundsexception:5
At Com.ray.ch04.Test.main (test.java:12)


4. In addition to the above explicit assignment, we can also use new to create an array, but we must write the length of the array clearly

Package Com.ray.ch04;public class Test {public static void main (string[] args) {int[] a2 = new int[5];//must have a clear length}}

There may be doubt as to why int can also use new because it is followed by "[]", which represents an int that is not of the underlying type, but instead represents an array of type int, which is an object, so you can use the new


5. Assigning values

If it is the underlying type, the data will be released directly into the array, and if it is a class, the reference will be placed inside the array.

Base type:

Package Com.ray.ch04;import Java.util.arrays;import Java.util.random;public class Test {public static void main (string[ ] args) {int[] a2 = new Int[5]; Random random = new random (int i = 0; i < a2.length; i++) {A2[i] = Random.nextint ();} System.out.println (arrays.tostring (A2));}}

Output:

[-1160871061,-1727040520,-1657178909,-765924271,-1625295794]


Object:

Package Com.ray.ch04;import Java.util.arrays;public class Test {public static void main (string[] args) {book[] a2 = new Bo ok[5];for (int i = 0; i < a2.length; i++) {A2[i] = new book (); System.out.println (arrays.tostring (A2));}} Class Book {}


Output:

[Email protected], [email protected], [email protected], [email protected], [email protected]]


Summary: This section briefly discusses several points of attention in arrays.


This chapter is here, thank you.

-----------------------------------

Directory



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding the initialization of the java-4.8 array from the beginning (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.