Java Basics Review-use demo to get to know arrays

Source: Internet
Author: User
Tags array length

Preface: The array is the basic data type which all programming languages involve, but often in the actual development time the array's direct use is still relatively few, uses the more is the array traversal thought, more simple says the logarithmic group object's for/while circulation.

Arrays are reference data types as well as class objects, where the underlying reference data type is: The same heap, which is pointed to by different stacks .

A: What is an array?

The problem seems simple, but not many people can tell the basic meaning of the array.

From Baidu's explanation: the so-called array, is a sequence of unordered elements. If a collection of variables of the same type is named, the name is an array name. Each variable that makes up an array is called the component of an array, also known as an element of an array, sometimes called a subscript variable. The numeric number of each element used for the area fraction group is called the subscript. An array is a form of organizing a number of elements of the same type in an unordered form in the program design for ease of handling. These unordered collections of homogeneous data elements are called arrays.

My understanding is that an array is an ordered set of data with the same data type.

The same data type, ordered (array index), collection

B: Creation of arrays

How to create an array

1 "Simple method: Data type array name [] = new data type [array length]; or data type [] Array name = new data type [array length];

Then assign values to the variables in the array, the array name [array index number] = the object to which the value is assigned;

Demo:

Note: 1. If you simply declare an array without assigning a value, all variables here are the default values for that variable type, such as int date[] = new int [2]; Then all variables in date are the default value of int 0;

Since the array is a reference data type, it is arbitrary to assign a value to the corresponding object. 2, the index range of the array is 0-----The length of the array-1;

1  PackageTop.ankermaker;2 /*3 * Administrators:ankermaker4  */5  Public classDatatest01 {6 7      Public Static voidMain (string[] args) {8         int[] data =NULL;9data =New int[3];TenData[0] = 10; OneDATA[1] = 20; ADATA[2] = 30; -         //array traversal, need to get the length of the array, here the dynamic method to get the length of the array -          for(inti = 0; I < data.length;i++) {     the System.out.println (Data[i]); -         } -     } -  +}
Output results: 10 20 30

The instantiation process for an array is as follows:

You can easily see from the above instantiation that the instantiation process of an array is similar to the instantiation of an object, or that all reference data types are created in the same way.

The following is a separate reference to the array that describes the reference between the values using:

Let's just remember this: all instantiation mappings for reference data type objects are in memory: The same heap is referenced by different stacks;

The code is as follows:

1  PackageTop.ankermaker;2 /*3 * Administrators:ankermaker4  */5  Public classDatatest01 {6      Public Static voidMain (string[] args) {7         int[] data =NULL;8data =New int[3];9Data[0] = 10;TenDATA[1] = 20; OneDATA[2] = 30; A         //to test references between array objects -         intTemp[] =NULL;//in the following I add a small description of this; -temp =data; theTEMP[0] = 99; -         //array traversal, need to get the length of the array, here the dynamic method to get the length of the array -          for(inti = 0; I < temp.length;i++) {     - System.out.println (Temp[i]); +         } -     } +  A}
Output results: 99 20 30

First , the int temp [] = null; if there is no reference between the arrays, the gods can no longer be called in the program, and once the call throws an exception, "The end is a simple description of common array exceptions ."

Back to this demo code combined with the following image we can simply understand the reference between the array is what is going on!

The process is as follows:

The same heap, which is pointed to by different stacks, refers to the two array name variables that point to the same heap object ;

C: Initialization of a static array

Static array initialization is actually the assignment of the variables in the array when the address is declared;

To create a method:

1 Simple method: Array type array name [] = {n/a};

2 Complete method (recommended this method): array type array name [] = new array type [] {one-way};

Why do we recommend the complete method, do you still remember the knowledge of anonymous objects? When using an anonymous array, you must have a new array type [] {n/a};

Therefore, it is recommended to use the complete method to reduce unnecessary discrimination.

Also use a Demo to know:

1         //Simple Method2         int[] Statedate = {1,3,4};3         4          for(inti = 0; I < statedate.length;i++) {    5 System.out.println (Statedate[i]);6         }7         8         //Full method (anonymous array)TenSystem.out.println (New int[] {The}.length); One         

To summarize:

The most disadvantage of the array is that the array length is fixed, if in the later development need to use the array to solve the problem, usually use a special data structure to implement a dynamic size of the array, (purely after their own study of the summary, we can comment on the comments, mutual learning is the purpose!) Thank you)!

expansion: Common exceptions when working with arrays are:

    • ArrayIndexOutOfBoundsException

The exception that is thrown when an array of numbers is accessed with an illegal index. Is the exception that is thrown when the array index value specifies an error or starting position for a given error, or if the number of data accessed exceeds the size range of the array.

For example: We initialize an array array[2], and when we access array[3], we throw the exception.

    • NullPointerException is the variable in the array that is not instantiated; for example: int data[] =null;
    • Arraystroeexception

This is the exception that is thrown when you attempt to store an object of the wrong type into an array of objects.

Example: Object x[] = new STRING[3]; X[0] = new Integer (0), the exception is thrown, and the solution is to store the array correctly.

Java Basics Review-use demo to get to know arrays

Related Article

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.