[Java Getting Started note] Java Language Basics (v): arrays

Source: Internet
Author: User
Tags array definition

Brief introduction

Arrays can be used to store multiple data, and the Java array requires that all array elements have the same data type. Once the array is initialized, the space in the memory of the array is fixed and the length is immutable, even if the elements of the array are emptied, the space occupied remains.

Life case: Natural History Racks each shelf is the same type of items, the length of the same, even if the items under the shelf, the shelf will remain unchanged. Defining arrays

Using an array of 4 steps:

1. Declare arrays in Java that support the format of the array definition in two formats:
Type [] variable name; type variable name [];

Cases:

int [] A; int b[];  // There are two ways to do it, but the first one is recommended. 

Declaring an array is also equivalent to buying a custom object Rack (array) in the example above, but not yet determining the size of the shelf (the size of the array), or putting something on the shelf (array elements).

2, allocating space allocation space is an array of initialization, initialization is divided into two ways: the first: static initialization static initialization of the syntax format as follows:
new data type []{data 1, data 2, Data 3, ...};

Cases:

int New int []{4,2,64,12,3};  // The numbers are stored in the array, the size of the array (length) is the number of elements in the curly braces, and the size of this example is 5 New string[]{"Zhang San", "John Doe", "Harry"};  // The type of all data must be a defined data type int [] c = {2,5,7};  // You can also use this shorthand method

This is like buying a custom-made shelf back, and putting the display in the rack to buy it back together, and the size of the rack is just how big the item will be. The second type: dynamic initialization

Dynamic initialization is the size of only the specified array, and the system assigns the initial value to each element in a way. The syntax format is as follows:

new data type [size];

In this format, you need to specify an array size for an integer type, specifying the length of the data, and how many elements the array can hold. Specify a default initial value for all elements at the same time.

Cases:

int []  newint[5];  // defines an array with a length of 5, that is, the ability to hold 5 data of type int, while the default 5 values are initialized to 0

The default values are set as follows when initializing:

    • Byte,short,int, long default is 0;
    • float, double default is 0.0;
    • char defaults to ' \u0000 ';
    • The boolean default is false;
    • Other reference types default to null;
3, assignment Although you have initialized the array, we can still change the data in it by assigning a value. So we've initialized the array and determined the size of the array, how do we assign a value to an array element? There is also a concept of subscript in the array, when the array is initialized, a subscript is assigned to each element, and it is the same as the number on the item display, and we can re-assign the value to each element by subscript. The subscript of the array starts at 0, that is, the subscript of the first element is 0, and as for why we should start with 0, let's look at the example of the assignment:
int []  newint[3];  // defines an array of type int of length 3, with 3 element values default to 0;a[0] = 4;  // set the first element to a value of 4a[1] = 5;  // set the second element to a value of 5a[2] = 7;  // set a third element value of 7

Why is the array subscript starting with 0?

The first thing we need to know is that the program is running in computer memory, and when our program starts processing the data, it opens up a little space in memory to store the data, and in the code it defines a variable, such as:

int a = 5;

Defines an int type variable with a value of five, which in memory is represented by allocating a small chunk of memory in memory, named A, with a value of 5.

And we are like through a such a variable name to get in memory worth it? Because every little piece of memory has an address, just like the home where we live has an address, through this address, through this address, you can know who is living inside.

Two arrays in memory are stored in a connected location, convenient operation of the array, we look at an example:

int New int [3];  // defines an array of type int with a length of 3 and an element with a default value of 0

This is the case in memory:

The same is true for data in arrays, how do you know the data for each element of the B array? First we know where the array in memory is through B, because the array is a contiguous memory space in memory, so the position of b[0] is equal to b+0, and b[1] is b+1,b[2]=b+2,...... And so on, you know where all the elements of the array are. and the first element in the array is exactly where the beginning of the array is, denoted by b[0], which is exactly where B is represented, and 1 for the position of the first element, which needs to be represented as a+1-1.

4. Processing data

In the preceding content, we have defined the array, initialized the array, assign the value, then how to use the data, in fact, we have already analyzed, we have an array subscript to assign values to the array, but also through the array subscript to get the value of each element.

int New int [3];a[0] = 3; a[1] = 4; a[2] = 5; System.out.println (a[1]);  // gets the value of the second position of the array, outputs it // We can also iterate through each element in the array  for (int i = 0; i < a.length; i++) {    System.out.println (a[i]);   // cycle through each element in the array }

In the example above, when we loop through the print, we see that the a.length is used, through the array variable name. Length can be obtained from the array, so above we get the length of the array by A.length 3, loop 3 times, print the elements of the group.

Non-existent subscript should not be accessed

As we know above, we control the array by assigning a value to the variable by the variable name [subscript], and if the access exceeds the subscript of the array length, an exception occurs.

int New int [2];a[5] = 2;  // an array with a length of only 2 length, and we access the 6th element by subscript 5, the actual a array does not exist in this position, there will be an exception. 

Two-dimensional arrays or multidimensional arrays

All we're talking about is storing a primitive data type or reference data type in an array, and we can also store an array in an array:

Above, we store another array element in an array element, which stores the value of a data type int, which we call a two-dimensional array.

Definition of a two-dimensional array, initialization and assignment
data type [] []  new data type [length 1][length 2];

Length 1 is the length of the outer layer of an array, length 2 bits inside the length of a layer.

In the form of definition and initialization, we can do this:

int New int [3] [3];a[0] [0] = 2;  // access is preceded by a number of square brackets indicating the element subscript of the outer layer array, the number of the second square bracket is the subscript of the array inside the a[0][1] = 3; a[0][2] = 4; a[1][0] = 5; a [1][1] = 3; a[] = 9; a[2][0] = n; a[2][1] = a[2][2] = 85;

We can also define a two-dimensional array as a format:

int[] A =New int[3] [];//set the outer layer length to 3 when defining initialization, without setting the length of the inside layer,
A[0] =New int[2];//initialize the inside of each layer, set the lengthA[1] =New int[1];a[2] =New int[3];a[0][0] = 3;//assign a value to each elementA[0][1] = 4; a[1][0] = 9; a[2][0] = 90; a[2][1] = 70; a[2][2] = 85;

And the three-dimensional array is the array inside the array is stored in the array ~~~~~ and so on can have n multidimensional array, but the average person use not much ~ ~ ~ ^_^ ~ ~ ~

[Java Getting Started note] Java Language Basics (v): arrays

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.