JAVA---arrays

Source: Internet
Author: User

A one-dimensional array is essentially a set of linear sets of the same type that can be applied when the program needs to process a set of data or pass data.

Create a one-dimensional array: An array as an object allows memory to be allocated using the New keyword. Before you use an array, you must first define the type to which the array variable belongs.

1:int arr[]; Declares an array of type int, and each element in the array is of type int.

String str[]; Declares an array of type string, with each element in the array being string.

Declaring an array simply gives the data type of the array name and element, and allocates memory space for the array to be really used. When allocating memory for an array, you must indicate the length of the arrays.

Array name =new array element type [number of array elements]; Arr=new Int[5];

An array is a subscript that distinguishes between different elements in an array, and the subscript of an array starts at 0.

2: Allocates memory for arrays while declaring the array.

Array element type array name =new array element type [number of array elements];

int month=new int[2];

Initialize a one-dimensional array:

int Arr=new int[]{1,3,2,5,8};

int arr={1,3,2,5,8};

eg

public class GetDay () {

public static void Main (String[]args) {

int Day=new int[]{31,28,30,31};

for (int i=0;i<day.length,i++) {

System.out.println ("day[i]");

}

}

}

Creation and use of two-dimensional arrays

If each element in a one-dimensional array is still an array, then it is a two-dimensional array. A two-dimensional array is often used to represent information in a table organized as columns and rows, the first subscript represents the row for the element, and the second subscript represents the column in which the element resides.

Two-dimensional arrays can be thought of as special one-dimensional arrays, so there are two ways to create a two-dimensional array.

1: Declare first, then use the new operator for memory allocation.

Array element type array name [];

array element type [] array name;

int arry[][];

As with the same-dimensional arrays, the two-dimensional arrays are declared without allocating memory space, and the new keyword is used to allocate memory before each element can be accessed.

For high-dimensional arrays, there are two ways to allocate memory for an array:

(1) Allocate memory space directly for each dimension.

A=new Int[2][4]

Allocate memory for each dimension, respectively,

A=new int[2][];

A[0]=new int[2];;

A[1]=new Int[3];

(2): allocating memory for arrays while declaring

The second way is the same function as the first implementation. When allocating memory for a two-dimensional code array, first specify the memory of the leftmost dimension, and then allocate memory separately to the remaining dimensions.

Two-dimensional array initialization:

Type Arryname[][]={value1,value2.....valuen};

int arryname[][]={{1,2},{45,10}};

Use a two-dimensional array:

public class matrix{

public static void Main (String[]args) {

int a[][]=new int[3][4];

for (int i=0;i<a.length;i++) {

System.out.println (A[i][j]);

}

}

}

For an integer two-dimensional array, the system assigns the initial value of 0 to each element in the array after the creation succeeds.

Basic Operations for arrays:

1: Iterating through an array

Traversing a two-dimensional array uses a double for loop.

public class trap{

public static void Main (String[]args) {

int b[][]=new int[][]{{1},{2,3},{4,5,6}};

for (int k=0;k<b.length;k++) {

for (int j=0;j<b[k].length;j++) {

System.out.println (B[k][j]);

}

}

}

}

It may be easier to use a foreach statement when iterating through an array.

public class tautog{

public static void Main (String[]args) {

int b[][]=new int[][]{{4,3},{1,2}};

SYSTEM.OUT.PRINTLN ("The element in the array is:");

for (int x[]:b) {

for (int y[]:x) {

if (x==b.length) {

System.out.println (y);

}

Else

System.out.println (e+ "\");

Populating a replacement array element

After the element definition in the array is complete, you can replace the elements in the group with fill in the Arry class. This method can be used to replace any type of array element by overloading the form.

Fill (int a[],int value): A is an array of elements to replace, and value is the data to be replaced. Assigns the specified int value to each element in the array.

Import Java.util.Arrays;

public class swap{

public static void Main (String[]args) {

int arr[]=new int[5];

Array.fill (arr, 8);

for (int i=0;i<arr.length;i++) {

System.out.println (Arr[i]);

}

}

}

Fill (int []a,int fromindex,int toindex,int value); This method assigns the specified int value to each element in the specified range. If Fromindex==toindex, the padding range is empty.

Import Java.util.Arrays;

public class displace{

public static void Main (String[]args) {

int Arr[][]=new int[][]{45,12,2,10};

Array.fill (arr,1,2,8);

for (int i=0;i<arr.length;i++) {

System.out.println (Arr[i]);

}

}

}

To sort an array:

The Arrays class static sort () method enables you to sort the array. The sort () method provides a number of overloaded forms that can be sorted in ascending order for any type of array.

Array.Sort (object)

Import Java.util.Arrays;

public class taxis{

public static void Main (String[]args) {

int arr[]={23,42,12,8};

Arrays.sort (arr);

for (int i=0;i<arr.length;i++) {

System.out.println (Arr[i]);

}

}

}

The above example is to sort the shaped array. The sorting algorithm for array of string types in Java is sorted according to the dictionary orchestration Order. So numbers precede letters, and uppercase letters precede lowercase letters.

To copy an array:

The CopyOf () method of the arrays class and the Copyofrange () method can be used to replicate the array, copyOf () is a copy of the arrays to the specified length, and the Copyofrange () method copies the specified length of the array into a new array.

CopyOf (Arr,int newlength);

Arr: the array to be copied; Newlength: refers to the length after the copy. If the length of the new array is greater than the length of the array arr, it is populated with 0 (depending on the type of the copied array to determine the padding value, the integer array determines to be filled with zeros, and the char array is padded with null). If the copied array length is less than the length of the array arr, the new array length is truncated as soon as the first element of the array arr is satisfied.

eg

Import Java.util.Arrays;

public class cope{

public static void Main (String[]args) {

int Arr[]=new int[]{23,42,12};

int newarr[]=arrays.copyof (arr,5);

for (int i=0;i<newarr.length;i++) {

System.out.println (Newarr[i]);

}

}

}

Copyofrange () Method: Copyofrange (Arr,int fromindex,int toindex); Arr: The array object to be copied. FromIndex: Specifies the index position at which to start copying the array. The fromindex must be between 0 and the entire number of leader degrees.  The new array includes the elements of the index Fromindex. Toindex: The last index position of the range to copy. Can be greater than the length of the array arr. The new array does not include an element whose index is toindex.

eg

Import Java.util.Arrays;

public class repeat{

public static void Main (String[]args) {

int Arr[]=new int[]{23,42,12,84,10};

int Newarr[]=array.copyofrange (arr,0,3);

for (int i=0;i<newarr.length;i++) {

System.out.println (Newarr[i]);

}

}

}

}

JAVA---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.