JAVA array, java Array

Source: Internet
Author: User
Tags array definition

JAVA array, java Array

Arrays are an important data structure for each programming language. arrays provided by java are used to store elements of the same type with a fixed size.

When you need to save a set of variables or objects with the same data type, we cannot define a variable name for each variable. Such an operation will make the code bloated, heavy workload, and meaningless. In this case, we need an array to save the data. Arrays can be divided into one-dimensional numbers, two-dimensional arrays, and multi-dimensional arrays according to different requirements. Next, let's take a look at the Declaration, creation, and initialization of arrays.

I,Array declaration

To use an array in a program, you need to declare the array first. The syntax for declaring the array variables is as follows:

One-bit array declaration method:

Type [] array name; (preferred method)

Type array name []; (same effect, but not recommended)

The length of the array cannot be specified during Declaration (number of elements in the array)

2. Create an array

Use the new keyword in Java to create an array. The syntax is as follows:

Method 1 (recommended, more representative of the current array type)

Type [] array name = new type [number of elements in the array];

Eg:

Int [] a = new int [3];

Array name, that is, application a, pointing to the first address of the array element.

Method 2 (initialize directly when defining)

Type [] variable name = new type [] {array elements separated by commas}

The red part can be removed, so there are two types:

1. int [] num = new int [] {1, 2, 3, 3 };

2. int [] num1 = {1, 3, 6, 87, 4 };

Int [] num = new int [] {1, 2, 3, 3}; the second square brackets cannot add an array length, because the number of arrays is determined by curly brackets.

Each array in Java has the length attribute, indicating the length of the array.

The length attribute is public final int, that is, the length is read-only. Once the length of the array is determined, the size cannot be changed.

  

Two-dimensional array definition method:

 1 int[][] table = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 5, 6, 3 } }; 2  3         int[][] table1 = null; 4         table1 = new int[3][3]; 5  6         int[][] table2 = new int[3][3]; 7         table2[0][0] = 11; 8         table2[0][1] = 121; 9         table2[0][2] = 11;10         table2[1][0] = 11;11         table2[1][1] = 121;12         table2[1][2] = 11;13         table2[2][0] = 11;14         table2[2][1] = 121;15         table2[2][2] = 11;16         System.out.println();

Traversal of two-dimensional arrays

1 for (int row = 0; row < table.length; row++) {2             for (int column = 0; column < table[row].length; column++) {3                 System.out.print(table[row][column] + "\t");4             }5             System.out.println();6 }
   3. array Initialization

 

1. Dynamic initialization: the array definition is separated from the operations for allocating space and assigning values to arrays;
2. Static initialization: when defining numbers, allocate space for array elements and assign values;
3. Default initialization: The array is a reference type, and its elements are equivalent to member variables of the class. Therefore, after the array is allocated space, each element is also initialized by the hermit according to the rules of the member variables.

 

Dynamic instance:

1 public class Arrays 2 {3 public static void main (String args []) {4 int a []; 5 a = new int [3]; 6 a [0] = 0; 7 a [1] = 1; 8 a [2] = 2; 9 String []; 10 string = new string [3]; 11 string [0] = "Zhang San"; 12 string [1] = "Li Si"; 13 string [2] = "Xiao Ming"; 14} 15}

Static instance:

1 public class Arrays 2 {3 public static void main (String args []) {4 int a [] = {0, 1, 2 }; 5 String [] string = {"Zhang San", "Li Si", "Xiao Min" };6} 7}

Default instance:

1 public class Arrays     2 {     3      public static void main(String args[]) {     4          int[]  a  = new int [5] ;     5          System.out.println("" + a[3]) ;     6      }     7 }   

Processing Array:

The array elements and the array size are determined, and there are multiple elements in the array. Therefore, when processing the array, we usually use basic for loop and enhanced for loop.

Example:

This instance fully demonstrates how to create, initialize, and manipulate Arrays:

1 public class Array {2 2 3 3 public static void main (String [] args) {4 double [] List = {1.9, 2.9, 3.4, 3.5 }; 5 5 6 6 // print all array elements 7 7 for (int I = 0; I <List. length; I ++) {8 8 System. out. println (List [I] + ""); 9 9} 10 10 // calculate the sum of all elements 11 11 double total = 0; 12 12 for (int I = 0; I <List. length; I ++) {13 13 total + = List [I]; 14 14} 15 15 System. out. println ("Total is" + total); 16 16 // find the maximum element 17 17 double max = List [0]; 18 18 for (int I = 1; I <List. length; I ++) {19 19 if (List [I]> max) {20 20 max = List [I]; 21 21 21} 22} 23 23 System. out. println ("Max is" + max); 24 24} 25 25}Array1.java enhanced for loop traversal Array

JDK 1.5 introduces a new type of loop, which can traverse the array without using the standard.

This instance is used to display all elements in the array myList:

1 public class Array {2 3 public static void main (String [] args) {4 int [] List = }; 5 6 // print all array elements 7 for (int a: List) {8 System. out. println (a); 9} 10} 11}

 

Array Replication

1. Use for loop Replication

1 public class ArrayCopy {2 3 public static void main (String [] args) {4 int [] number = {11, 2, 23, 12, 4, 34, 5 }; 5 int [] number1 = new int [number. length]; 6 7 if (number! = Null & number. length> 0) {8 for (int I = 0; I <number. length; I ++) {9 number1 [I] = number [I]; 10} 11 12 for (int value: number1) {13 System. out. print (value + "\ t"); 14} 15} 16} 17}Array2.java

2. Copy the Array Using the copyOf Method

1 public class Text {2 public static void main (String [] args) {3 int [] a = {2, 5, 2, 2, 6, 2, 2 }; 4 5 Text t = new Text (); 6 int [] m = t. copyOf (a,. length); 7 for (int n: m) {8 System. out. print (n + ""); 9} 10 11 System. out. println (); 12 13 int [] B = Arrays. copyOf (a,. length); 14 for (int n: B) {15 System. out. print (n + ""); 16} 17} 18 public static int [] copyOf (int [] B, int newLength) {19 return B; 20} 21}Array3.java

3. Copy the specified range of the specified array to a new array (copyOfRange)

1 public class Text {2     public static void main(String[] args) {3         int[] a = { 2, 4, 2, 77, 22, 777, 34 };4         int[] b = Arrays.copyOfRange(a, 0, 3);5         for (int n : b) {6             System.out.print(n + " ");7         }8     }9 }

 

Array sorting 1 public class Bubble {2 3 public static void main (String [] args) {4 int a = 100; 5 int B = 46; 6/* 7 * int temp = 0; temp = a; a = B; B = temp; 8 */9/* 10 * a = a + B; B = a-B; a = a-B; 11 */12/* 13 * a = a ^ B; B = a ^ B; a = a ^ B; 14 */15 16 System. out. println ("a = 46, B = 100" + "\ t" + a + "" + B); 17 18 // Bubble Sorting 19 int [] number = {11, 2, 23, 12, 4, 34, 5}; 20 // 11, 2, 23, 12, 4, 34,521 22 System. out. println ("Before sorting:"); 23 for (int num: number) {24 System. out. print (num + "\ t"); 25} 26 27 for (int I = 0; I <number. length; I ++) {// control the elements in the array 28 for (int k = 0; k <number. length; k ++) {29 if (number [I]> number [k]) {30 int temp = 0; 31 temp = number [I]; 32 number [I] = number [k]; 33 number [k] = temp; 34} 35} 36} 37 38 System. out. println ("\ n after sorting:"); 39 for (int num: number) {40 System. out. print (num + "\ t"); 41} 42} 43}Array4.java

Sort by sort

 1 public class Text { 2     public static void main(String[] args) { 3         int[] a = { 2, 4, 2, 77, 22, 777, 34 }; 4         Arrays.sort(a); 5         for(int b:a){ 6             System.out.print(b+" "); 7         } 8     } 9     10 }

 

Generate a random array that is not repeated. 1 public class NoRepeatArray {2 public static void main (String [] args) {3 System. out. println (); 4 Random rd = new Random (); 5 System. out. println (rd. nextInt (5) + 1); // a random number between 0-9 6 7 // 5 Non-repeated random numbers are generated from 1-5 8 int [] numbers = new int [5]; 9 // define the index of the storage value array 10 int index = 0; 11 System. out. println ("------------------------------- \ n"); 12 for (int I = 1; I <= 5; I ++) {13 Random rd1 = new Random ( ); 14 int num = rd1.nextInt (5) + 1; 15 16 if (numbers [0]! = 0) {17 boolean flag = false; // controls whether repeated elements 18 19 // traverses the elements in the production array 20 for (int j = 0; j <numbers. length; j ++) {21 22 if (num! = Numbers [j]) {23 flag = true; 24} else {25 flag = false; 26 break; 27} 28} 29 30 if (flag = true) {31 numbers [index ++] = num; 32} else {33 34 // duplicate elements are found to generate a new random number 35 I --; 36} 37 38} else {39 numbers [index ++] = num; 40} 41 42} 43 44 for (int val: numbers) {45 System. out. print (val + "\ t"); 46} 47} 48}Array5.java

 

 

Array comparison

If the two arrays contain the same elements in the same order, the two arrays are equal. In addition, if both array references areNullThey are considered to be equal.

 1 public class Text { 2     public static void main(String[] args) { 3         int[] a = { 2, 4, 2, 77, 22, 777, 34 }; 4         int[] b = { 2, 4, 2, 77, 22, 777, 34 }; 5         int[] c = { 4, 2, 22, 77, 2, 777, 34 }; 6          7         boolean flag = Arrays.equals(a, b); 8         System.out.println(flag); 9         10         boolean flag1=Arrays.equals(a, c);11         System.out.println(flag1);12     }13 }

The above example compilation and running results are as follows:

true
false

 

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.