----arrays in the Java language

Source: Internet
Author: User

DAY07--Arrays in the Java language

First, an overview of the array:

What is an array? What is an array for? Why do you want to learn arrays? These are all questions. Once you've learned the array, you'll know that an array is a collection of elements stored in a single data type in an adjacent memory location. What is a single data type? That is, when you define an array of data types, you can only store an array of that type. So when we visit him, we can directly access the array elements by specifying the name and subscript of the array.


Two, array:

1. Defining arrays

Data type array name [] = new data type [memory data size] int a[] = new INT[100]

2, the classification of the array:

Define a one-dimensional array: An array that stores only a single row.                    int a[] = new INT[100];            Define a two-dimensional array: is the array of multiple rows and columns stored. int b[][] = new INT[100][100];

3. Two methods for defining arrays:

Instance 1:1-dimensional array

        package www.com;         public class arreay {    public static void main (String[] args)  {    //method One, the common method defines a one-dimensional array     int b[] =  new int[5];        //assigns a value to the defined array. Assign a value starting with subscript 0     b[0] = 10;    b[1] = 20;     b[2] = 30;    b[3] = 40;    b[4]  = 50;        //the value of the output array     system.out.println (b [2]); /system.out.println (b[5]);//This is wrong, the control panel will output an index exception.     //method Two, the declaration array is the same assignment value.     int c[] ={1,2,3,4,5,6,7,8};         System.out.println ("The length of the array" +c.length);//Gets the length of the array   &nbsP; system.out.println (c[3]);//output of the fourth array.     //iterates through the array. 1. Use for loop traversal.     for  (int i=0; i<7; i++ )  {system.out.print (c[i]+ ","); &NBSP;&NBSP;&NBSP;&NBSP;}//2, use the For each loop to iterate through the array.     for  (int i : c)  {system.out.print (i+ ". ");     }     //find the largest number     int dzx[]  = {10,20,30,40,50,60,70};    int max = dzx[0];     for  (int i = 1; i < dzx.length; i++)  {if  (Dzx[i]>max)  max = dzx[i];    }system.out.print ("Max:" +max);             }      }

     instance 2:2-D array

     package www.com;     public class array2  {        public static void main (String[]  args)  {    //Common method One, defines an array of 3 rows defined by a two-dimensional array int a[][] = new int[3][4];//     //Assignment Value a[0][0] = 1;a[0][1] = 2;a[0][2] = 3;a[0][3] = The  4;a[1][0] = 5;a[1][1] = 6;a[1][2] = 7;a[1][3] = 8;//is assigned in turn. I haven't finished the assignment yet.     //output System.out.println (a[0][3]);//Method Two, define a two-dimensional array int b[][] ={ {1,2,3,4,5},{ 6,7,8,9,10}};//Comma separated, each curly brace represents a row.     //output System.out.println (b[1][1]); SYSTEM.OUT.PRINTLN ("Output" +b.length+ "line!") But not ripe length Oh! ");//Output Total row number System.out.println (b[1].length);//output the length of a line.     //traversing a two-dimensional array://Using a For loop.     for  (int i = 0; i < b.length; i++)  { for  (int j = 0; j < b[i].length; j++)  {             system.out.print (b[i][j]+ " ");}     }    }    }


Third, concluding remarks:

Here you should understand what an array is! You can interpret the array as data stored in the "grid". Each lattice has his coordinates, and the data in each of the squares is read over and over again as you go through it.

This article from "Lonely One Night" blog, reproduced please contact the author!

----arrays in the Java language

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.