An array of basic Java learning

Source: Internet
Author: User
Tags array length

1. What is an array

Arrays are collections of elements of the same data type, and array elements are arranged in a linear order.

When each element of an array is represented, the array's position is represented by the subscript of the arrays, where the subscript 0 is the first element of the array. The element subscript of the last array is the array length minus one.

The syntax for declaring an array is: data type [] Array name = new data type [size]; for example:

New STRING[10];

After the array is defined, the array has a default initialization value, and when we do not indicate the initialization value for it, the int default initialization value is 0;string default initialization value is null,double default initialization value is 0.0, etc.

One of the two-dimensional arrays is that the array elements are one-dimensional elements of a one-dimensional array. Cases:

int[][] in = int[3][2];//can be understood as this is a one-dimensional element with three elements, where each element value is a one-dimensional element

2. Array Access

By using an array, we can get the length of the array and the element values for each position of the array.

For example:

New String[10]; int len = str.length (); // get the length of the array System.out.println (Str[7]); // gets the element value labeled 7 in the array, which is the eighth element of the array

3. Classic extensions of arrays

 1.99 Multiplication Table

classmultiplicationtable { Public Static voidMain (string[] args) {//I variables are used to control the number of rows         for(inti = 1; I < 10; i++) {            //J variables are used to control the maximum number of participating calculations in each row: equal to the number of rows             for(intj = 1; J <= I; J + +) {                //format the output and use "\ T" to control the layout alignmentSystem.out.print (j + "*" + i + "=" + J*i + "\ T")); }            //after each line is finished, a newline is requiredSystem.out.println (); }    }    }

This is the representation of a common two-dimensional array, the outer loop controls the number of rows, and the inner loop control column count.

2. Bubble sort

Bubble sort, is a simpler sort algorithm. In the bubble sort algorithm, you need to repeat the sequence of visits to sort, compare two elements at a time, and swap them out if they are in the wrong size order. Since the decimal is always placed forward in the sorting process, the large number is placed backwards, which is the equivalent of bubbles rising, so called bubble sort.

classBubblesort { Public Static voidMain (string[] args) {//Create an array        int[] arr =New int[10]; Random ran=NewRandom ();  for(inti = 0; i < arr.length; i++) {Arr[i]= Ran.nextint (100);        } System.out.println (Arrays.tostring (arr)); //Bubble SortSystem.out.println ("----------bubble sort Start----------");  for(inti = 0; i < arr.length-1; i++) {             for(intj = 0; J < arr.length-i-1; J + +) {                if(Arr[j] > arr[j + 1]) {                    intt =Arr[j]; ARR[J]= Arr[j + 1]; Arr[j+ 1] =T;        }} System.out.println (Arrays.tostring (arr)); } System.out.println ("----------Bubble sort End----------");    System.out.println (arrays.tostring (arr)); }}

An array of basic Java learning

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.