An array of preliminary Java learning

Source: Internet
Author: User

(1) definition of an array

An array is a data structure that is a collection that has the same type.

(2) syntax

There are three ways to define an array in Java:

① data type [] array name ={according to type initialization};

Int[] str1={1,2,3,3,4}; This method is initialized directly at the time of definition and, of course, determines its space size.

② data type [] array name =new data type [array space size];

int[] str2=new int[100];

Note: space size must be indicated here

③int[] str3=new []{1,2,3,4,5}; This method, like the first one, is initialized directly.

Assigning a value to an array

Assign a value to it by an array subscript: str2[0]=1;

Note: array subscript starts from 0;

Log Group Access

Like assignment, access is also done by array subscript

We can directly use one of these values int a= str1[0];

Logarithmic group traversal

(1) using a For loop

For (int I=0;i<str1.length;i++) {code block}

(2) There is a special method in Java

Foreach

For example: for (int i:str1) {System.out.println (i);}

Note: will traverse from head to tail


This article is from the "popular tastes" blog, make sure to keep this source http://asalawys.blog.51cto.com/12008007/1863184

An array of preliminary Java learning

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.