Java Learning Notes (iii)

Source: Internet
Author: User

Array

An array is a set of data that each of the data in an array is called an element. Any type of element can be stored in an array, but the type of elements stored in the same array must be identical.

First, the definition of the array

In Java, you can use the following format to define an array. As follows

new data type [number of elements or array length];

 How to define an array and access the elements in an array

1 Public classArrayDemo01 {2 Public Static voidMain (string[] args) {3int[] arr;//declaring Variables4 arr =New int[3];//creating an Array object5 System.out.println ("arr[0]=" + arr[0]);//accessing the first element in an array6 System.out.println ("arr[1]=" + arr[1]);//accessing the second element in an array7 System.out.println ("arr[2]=" + arr[2]);//accessing the third element in an array8 System.out.println ("The length of the array is:" + arr.length);//Print Array Length9    } 10}

The operation results are as follows

Second, the traversal of the array

When manipulating arrays, it is often necessary to sequentially access each element in the array, which is called an array traversal.

 Public class ArrayDemo04 {    publicstaticvoid  main (string[] args) {        int  //  define an        array / / Use the For loop to iterate over an array of elements for         (int i = 0; i < arr.length; i++) {            //  access element through index      }}}
1. Array FAQs

1.1 Array Maximum Value

 Public classArrayDemo05 { Public Static voidMain (string[] args) {int[] arr = {4, 1, 6, 3, 9, 8};//define an arrayintmax = arr[0];//define variables Max is used to remember the maximum number, first of all assuming the first element is the maximum value//the following iterates through the elements in the array through a for loop         for(intx = 1; x < arr.length; X + +) {if(Arr[x] > Max) {//Compare Arr[x] value is greater than Maxmax = arr[x];//conditional, assign the value of Arr[x] to Max}} System.out.println ("max=" + max);//Print Maximum value    }}

1.2 Array exceptions

The index of each array has a range, that is, 0~length-1. When accessing elements of an array, the index cannot exceed this range, or the program will error.

1Publicclass2 public    staticvoid3        int  newint//  define an array of length 4 4        //  Access array elements by subscript 4 5    6}

The error message that is indicated in the run result is an array out-of-bounds exception arrayindexoutofboundsexception, the reason for this exception is that the length of the array is 4 and its index range is 0~ 3, while the 4th line of code in the preceding code uses the index 4来 to access the element beyond the array's index range.

2. Two-dimensional array

Multidimensional arrays can be simply understood to be nested arrays in an array, and a two-dimensional array is more common in the program.

2.1 How two-dimensional arrays are defined

The first way:

int New int [3] [4];

The second way:

int New int [3] []; // three rows with indeterminate number of columns

The Third Way:

int [] arr = {{1,2},{3,4,5,6},{7,8,9}};//defines and assigns values

2.2 Two-dimensional array element traversal and array element summation and

classArrayDemo09 { Public Static voidMain (string[] args) {//summation and traversal of one-dimensional arrays        int[] arr = {10,20,30,40,50}; intsum = 0;  for(inti=0; i<arr.length; i++) {              //System.out.println (Arr[i]);Sum + =arr[i];} System.out.println ("Sum=" +sum); System.out.println ("---------------------"); //summation and traversal of two-dimensional arrays        int[] arr2 = {{1,2},{3,4,5},{6,7,8,9,10} }; intsum2 = 0;  for(inti=0; i<arr2.length; i++) {             for(intj=0; j<arr2[i].length; J + +) {                 //System.out.println (Arr2[i][j])Sum2 + =Arr2[i][j]; }} System.out.println ("Sum2=" +sum2); }}

      

Java Learning Notes (iii)

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.