Fourth. Flow control and Array

Source: Internet
Author: User

Note: Ending a loop with a break (you can jump to the label)

Use continue to ignore this loop to the remaining statement (you can also skip to the label)

Using the return End method

  

-type Array

An array is also a data type, which is itself a reference type

When defining the general use of type[] arrayname, this way is relatively easy to understand and formal

The array must be initialized before it can be used

Initialization: Static initialization arrayname = new Type{element1,element2,element3 ...}

Dynamic initialization arratname = new Type[length]

You cannot use both static and dynamic initialization: Specifying both the length of the array and the initial values for each array element

Using arrays

If the access array element is a specified index value of less than 0, or greater than the length of the array, an exception occurs at run time:

Java.lang.arrayindexoutofboundsexception:n (N is the array index you are trying to access)

-A Foreach Loop

for (Type Variablename:array | collection) {      //variablename iterates over each element automatically          }

When using a Foreach loop, you cannot change the value of an array element, so you cannot assign a value to a set of foreach arrays

-depth Array

Array elements and arrays of variables are stored separately in memory.

An array reference variable is simply a reference, and this reference variable can point to any valid memory, and the array element can be accessed through the array variable only if the reference points to valid memory

Reference variables are the fundamental way to access real objects

  

The actual array object is stored in heap memory, and if the array reference type referencing the array object is a local variable, it is stored in stack memory

Array reference variables are the fundamental way to access array elements in heap memory

  

If there is no longer any reference variable in the heap memory pointing to itself, then this array will be garbage collected by the garbage collection mechanism. So if you want to reclaim the memory space of an array, you can assign the array variable to NULL

 

Fourth. Flow control and Array

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.