Arrays in Java

Source: Internet
Author: User
Tags array sort comparable

in the Java language, an array is one of the simplest composite data types with very high frequency. An array is a set of ordered data that is slightly different from a collection in Java, where each element of the array has the same data type, which can be used to store data of the base type, or to store data of the reference data type. You can use a uniform array name and subscript to uniquely determine the elements in the array. It is worth noting that the array itself is a reference type, for example, int is a basic type, but int[] is a reference type, the array has a one-dimensional array and a multidimensional array.

One-dimensional arrays:

       1. Definition of one-dimensional array

        type[] arrayname;

For example:
int[] Intarray;
date[] Datearray;
         Sometimes we'll see a way to define it. < Span style= "color: #000000;" >type arrayname[]; This method is not recommended for use ...

2. Initialization of a one-dimensional array

int[] arr;

arr = new int[]{1,2,3,4}; The shorthand format is as follows:int[] intarray={1,2,3,4};
string[] stringarray={"A", "B", "C"};

2.2 Dynamic initialization: The programmer only specifies the length of the array at initialization, and the system assigns the initial values to the elements in those arrays .
1) Arrays of simple types
int[] Intarray;
intarray = new Int[5];

2) Array of composite types
string[] Stringarray;
String Stringarray = new string[3];/* a reference to each element in the array
Space (32-bit) */
stringarray[0]= New String ("a");//Open space for the first array element
stringarray[1]= New String ("B");//Open space for the second array element
stringarray[2]= New String ("C");//Open space for the third element of the array

3. A reference to a one-dimensional array element

the array elements are referenced in the following way:
Arrayname[index]

index is an array subscript, which can be an integer constant or an expression, and the subscript starts at 0. Each array has a property length that indicates its lengths, for example: Intarray.length indicates the length of the array intarray.

Multidimensional Arrays:

in Java, in fact, multidimensional array is in the array nested arrays, in fact, and many other languages are the same, such as js,as3,c and so on.

1. Definition of a two-dimensional array

type[][] arrayname;

2. Initialization of two-dimensional arrays

2.1 Static Initialization
int[][] intarray={{1,2},{2,3},{3,4,5}};

in the Java language, because the two-dimensional array is considered an array of arrays, the array space is not continuously allocated, so the size of each dimension of the two-dimensional array is not required.

2.2 Dynamic Initialization
1) Allocate space directly for each dimension, in the following format:
arrayname = new Type[arraylength1][arraylength2];
int[][] A = new int[2][3];

2) Allocate space for each dimension starting from the highest dimension:
arrayname = new type[arraylength1][];
arrayname[0] = new TYPE[ARRAYLENGTH20];
arrayname[1] = new TYPE[ARRAYLENGTH21];
... ..
Arrayname[arraylength1-1] = new TYPE[ARRAYLENGTH2N];

3) Example:
The dynamic initialization of an array of two-dimensional simple data types is as follows,
int[][] A = new int[2][];
a[0] = new Int[3];
a[1] = new INT[5];

for arrays of two-dimensional composite data types, you must first allocate the reference space for the highest dimension, and then allocate the space in the lower dimension sequentially.
Also, you must allocate space for each array element individually.

For example:
string[][] s = new string[2][];
s[0]= New string[2];//Allocating reference space for the highest dimension
s[1]= New string[2];//Allocate reference space for the highest dimension
s[0][0]= New String ("good");//Allocate space separately for each array element
s[0][1]= New String ("Luck");//Allocate space separately for each array element
s[1][0]= New String ("to");//Allocate space separately for each array element
s[1][1]= New String ("You");//Allocate space separately for each array element

3. A reference to a two-dimensional array element
  
for each element in a two-dimensional array, the reference is: Arrayname[index1][index2]
example: num[1][0];

4. Examples of two-dimensional arrays:
regular two-dimensional arrays:

public class Study {

public static void Main (String args[]) {

Int[][] A = {{1, 2}, {3, 4},{5, 6}};

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

for (int j = 0; j<a[i].length; J + +) {

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

}

}

}

}

Irregular two-dimensional arrays:

public class Study {

public static void Main (String args[]) {

Int[][] B = {{1, 2}, {3, 4, 5},{5, 6, 7, 8}};

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

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

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

}

}

}

}

Array of important methods:

One, padding array: Arrays.fill () method

Cons: Populating the data single.

Usage 1: Accepts 2 parameters

Arrays.fill (A1, value);

Note: A1 is an array variable, and value is the values of the element data type in a A1, which acts: each element in the fill A1 array is value

For example:

public class Study {

public static void Main (string[] args) {

Int[] A = new int[5];

Arrays.fill (A, 1);

for (int i:a) {

System.out.println (i);

}

}

}

Usage 2: Accepts 4 parameters

The first parameter refers to an array of operations, the second and third refers to the insertion of a fourth parameter in an area of the array, the second parameter refers to the starting element subscript (including the subscript), the third parameter refers to the end subscript (without the subscript), note: The array subscript for Java starts with 0

For example:

public class Study {

public static void Main (string[] args) {

Int[] A = new int[5];

Arrays.fill (A, 1);

Arrays.fill (A, 1, 3, 2);

for (int i:a) {

System.out.println (i);

}

}

}

Second, copy array: Clone () method

Clone () method, limit: Copy all, cannot copy partially.

public class Study {

public static void Main (string[] args) {

Int[] A = new int[5];

Int[] B;

Arrays.fill (A, 1);

b = A.clone ();

for (int i:b) {

System.out.println (i);

}

}

}

Three, compare arrays: Arrays.equala () method

Comparable interface: CompareTo () method, the class that implements the comparable interface has its own comparison function

Comparator interface: Compare () method and Equals () method

Generally only implement the compare () method to write a custom comparison method

Example:

Define the person class and compare the class

public class Person {

String FirstName, LastName;

Boolean sex;

int age;

Public person (string firstname, string lastname, Boolean sex, Integer age) {

Super ();

This.firstname = FirstName;

This.lastname = LastName;

This.sex = sex;

This.age = age;

}

Public String Getfirstname () {

return FirstName;

}

public void Setfirstname (String firstname) {

This.firstname = FirstName;

}

Public String Getlastname () {

return lastname;

}

public void Setlastname (String lastname) {

This.lastname = LastName;

}

Public Boolean Getsex () {

return sex;

}

public void Setsex (Boolean sex) {

This.sex = sex;

}

Public Integer Getage () {

return age;

}

public void Setage (Integer age) {

This.age = age;

}

}

Implementing comparator, defining a custom comparer

Import Java.util.Comparator;

public class Personcomparator implements comparator<person> {

@Override

public int Compare (person arg0, person arg1) {

if (Arg0.getage () > Arg1.getage ()) {

return-1;

}

return 1;

}

}

Test Comparator

public class Study {

public static void Main (string[] args) {

Person[] p = {

New Person ("Ouyang", "Feng", Boolean.true, 27),

New Person ("Zhuang", "GW", Boolean.true, 26),

New Person ("Zhuang", "GW", Boolean.false, 28),

New Person ("Zhuang", "GW", Boolean.false, 24)};

Arrays.sort (P, new Personcomparator ());

for (person Person:p) {

System.out.println (Person.getfirstname ());

System.out.println (Person.getlastname ());

System.out.println (Person.getage ());

System.out.println (Person.getsex ());

System.out.println ();

}

}

}

Four, array sort: arrays.sort () method

V. Finding arrays: Arrays.binarysearch () method

Cons: The array must already be sorted, otherwise the result cannot be expected

public class Study {

public static void Main (string[] args) {

Int[] A = {1, 2, 4, 5, 3};

Arrays.sort (a);

System.out.println (Arrays.binarysearch (A, 3));

}

}

VI. Display array data

1) An array holds objects, such as strings: You can use the Arrays.aslist method to convert an array to a list container after it is displayed.

2) data is basic type: can use for (int i:array) System.out.println (i); display

3) arrays.tostring () method

public class Study {

public static void Main (string[] args) {

Int[] A = {1, 2, 4, 5, 3, 1};

System.out.println (Arrays.tostring (a));

for (int i:a) {

System.out.println (i);

}

}

}

Arrays in Java

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.