Java Syntax Basics-arrays

Source: Internet
Author: User


1. Concept

a collection of the same type of data.  The array is actually a container .

2. Benefits of Arrays

The elements in the array can be automatically numbered starting with 0, allowing them to be manipulated.

3. format 1:

element type [] array name = new element type [ element number or array length ];

int[] arr = new int[5];

4. format 2:

element type [] array name = new element type []{ element, element, ... };

I nt[] arr = new int[]{3,5,1,7};

int[] arr = {3,5,1,7};

Element type [] array name = new element type [ element number or array length ];

int x = 4;//a data.        NEW: The keyword used to create the entity int[] arr = new INT[3]; A variable that defines an array of type int elements arr,//creates an array entity with the New keyword that can store 3 elements of type int.


Allocation of arrays in memory

Please see attachment


FAQ for Arrays

Array Operations FAQ.    1.//arrayindexoutofboundsexception: Array angle label out of bounds exception, occurs at runtime int[] arr = new INT[3];    System.out.println (Arr[3]);. This exception occurs when a corner label that does not exist is accessed.    2.//nullpointerexception null pointer exception.    arr = null;    System.out.println (arr[1]); This exception occurs when the reference (ARR) does not have any entities pointing, and its action entity is also being used.


For example, the common operation of an array - the maximum value

Public static int getmax (Int[] arr) {&NBSP;&NBSP;&NBSP;&NBSP;//1, defines a variable and records a larger value.     int max = arr[0];//initialization is an array of any element     //2, traversing an array.     for  (int x=1; x<arr.length ;x++ )     { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//3, make a judgment comparison.         if (Arr[x]>max)              max = arr[x];     }       return max;  }  //the second maximum value. PUBLIC&NBSP;STATIC&NBSP;INT&NBSP;GETMAX2 (Int[] arr) {    int maxindex = The  0;   //is initialized to any of the corner labels in the array.     for  (int x=0; x<arr.length ;x++ )     {         if (Arr[x]>arr[maxindex]) {&NBSP;&NBSP;&NBSP;&Nbsp;     maxindex = x;        }} 



Java Syntax Basics-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.