Dark Horse Programmer-java Array of foundations

Source: Internet
Author: User

                                    Array                            

   

An array is a set of data that has the same data type. For example, the collection of ball games-football, basketball, badminton, etc., electrical collection-televisions, washing machines, electric fans and so on. In programming, these collections can be referred to as arrays. Each element in the array has the same data type. In Java, an array is also considered an object, although the base data type is not an object, but an array of basic data types is an object. The introduction of arrays in programming can be used to manage and manipulate data more efficiently. The array can be divided into one-dimensional arrays, two-dimensional arrays according to the dimensions of the array ...

One-dimensional arrays:

1, the establishment of the array:

Arrays as objects allow memory allocation using the New keyword. Before using an array, you must first define the type to which the array variable belongs-declaring an array, declaring a one-dimensional array in two forms, and the syntax is as follows:

Array element type array name [];

array element type [] array name;

Array element type: Determines the data type of the array, which can be any data type in Java, including basic data types and non-basic data types.

Array name: For a valid identifier

Symbol "[]": Indicates that the variable is an array type variable, and a single "[]" indicates that the array to be created is a one-dimensional array

2, initialization of the array:

Arrays can be initialized with the same primitive data types, and the initialization of an array initializes each element of the array separately. There are two types of initialization of arrays.

Two methods initialize a one-dimensional array, as shown in the sample code:

int arr[] = new int[]{1,2,3,5,25}; //The first type of initialization

int arr2[] = {34,23,12,6}; //second type of initialization method

The array is initialized by including the data type inside the curly braces, separating the values of the array elements with commas, and the system automatically allocates a certain amount of space to the group. The first type of initialization, which creates an array of 5 elements, with values of 1, 2, 3, 5, 25, and the second initialization, creates an array of 4 elements with a value of 34, 23, 12, 6, in turn.

Example: Create a getday in a project, create an array of type int in the main method, and implement a number of days for each month output.

public class GetDay

{

Public static void Main (string[] args)

{

int Day[]=new int[]{,------------------------------ 31};// Create and initialize an array

for (int i = 0; i < i++)// use loops to output information

 {

System.out.println ((i + 1) + "month with" + Day[i] + "Day");// Output Information

 }

 }

}

Two-dimensional arrays:

1, The establishment of the array:

There are two ways to declare a two-dimensional array, the syntax is as follows:

Array element type array name [];

array element type [] array name;

Array element type: Determines the data type of the array, which can be any data type in Java, including basic data types and non-basic data types.

Array name: For a valid identifier

Symbol "[]": Indicates that the variable is an array type variable, two "[]" means the array to be created is a two-dimensional array.

2, initialization of the array:

Initializing a two-dimensional array is similar to initializing the same-dimensional array, and you can also use curly braces to initialize the two-dimensional array. The syntax is as follows:

Type arrayname[][] = {Value1,value2...valuen};

Type: Array data type

Arrayname: array name, a valid identifier

Value: The values of the elements in the array

Example: Create a class matrix in a project, write code in the main method to output a matrix of 3 rows and 4 columns and all elements are 0.

public class Matrix

{

Public static void Main (string[] args)

{

int a[][] = new INT[3][4];// define two-dimensional arrays

for (int i = 0; i < a.length; i++) {

for (int j = 0; J < A[i].length; J + +) {//iterate through each element in the array

System.out.print (a[i][j]);// to output the elements in an array

 }

System.out.println ();// Output spaces

 }

 }

}

Dark Horse Programmer-java Array of foundations

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.