Java Basics---How to use arrays in Java (30)

Source: Internet
Author: User

How to use arrays in Java

Working with arrays in Java requires only four steps:

1. Declaring arrays

Syntax: 650) this.width=650; "src=" http://img.mukewang.com/5371e74b0001b3c301590034.jpg "/> or 650) this.width=650;" src= "Http://img.mukewang.com/5371e782000148d101630029.jpg"/>

Where the array name can be any valid variable name, such as: 650) this.width=650; "src=" http://img.mukewang.com/5371e91d000147e903790167.jpg "/>

2. Allocate space

In short, it is the maximum number of elements that can be stored in the specified array

Syntax: 650) this.width=650; "src=" http://img.mukewang.com/5371ea810001a62d02760032.jpg "/>

Where the array length is the number of elements that can be stored in an array, such as: 650) this.width=650; "src=" http://img.mukewang.com/5371ecc10001c97804260171.jpg "/>

In other words, we can also merge the above two steps, allocating space for it while declaring the array, such as: 650) this.width=650; "src=" http://img.mukewang.com/5371ed4200012bee03590033.jpg "/>

3. Assign Value

After allocating space, the data can be placed into the array, and the elements in the array are accessed by subscript, for example, to scores an array of student scores 650) this.width=650; "Src=" http://img.mukewang.com/ 5371ed9f0001accb02100053.jpg "/>

4. Working with data in arrays

We can manipulate and manipulate the array after the assignment, such as getting and outputting the value of the element in the array 650) this.width=650; "Src=" http://img.mukewang.com/5371eec50001332707460043.jpg " />

Another way to create an array directly in Java is to declare an array, allocate space, and assign a merge complete, such as 650) this.width=650; "Src=" http://img.mukewang.com/ 5371ee8b000101aa04540056.jpg "/> It is equivalent to: 650) this.width=650;" Src= "http://img.mukewang.com/ 5371ef0f0001528c04830059.jpg "/>

Code:

public class HelloWorld {
public static void Main (string[] args) {

Defines a string array of length 5, saving test account information
String[] subjects = new string[5];

Assign a value to an element in the array, respectively
Subjects[0] = "Oracle";
SUBJECTS[1] = "PHP";
SUBJECTS[2] = "Linux";
SUBJECTS[3] = "Java";
SUBJECTS[4] = "HTML";

System.out.println ("The first 4 accounts in the array are:" + subjects[3]);
}
}

Operation Result:

The first 4 accounts in the array are: Java


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547964

Java Basics---How to use arrays in Java (30)

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.