Data Structures-arrays

Source: Internet
Author: User

Data Structures-arrays


For the most recent high-level languages, arrays are one of the most important data structures, although the implementation and processing of the different languages are not the same. Here's an array in the Java language.

You can first declare an array variable, such as numbers[100], instead of declaring the 100 independent variable number0,number1,....,number99 directly.


Declaring an array variable

You must declare an array variable to use the array in your program. Here's how to declare an array variable:


Datatype[] Arrayrefvar; The preferred method

Or

DataType arrayrefvar[]; Same effect, but not preferred method


Instance

Here are the code examples for both of these syntaxes:


Double[] myList; The preferred method

Or

Double mylist[]; Same effect, but not preferred method


Create an array

Use the new operator to create an array, as follows:


Arrayrefvar = new Datatype[arraysize];


The above statement does two things:

First, an array was created using datatype[arraysize].

Assign the reference of the newly created array to the variable Arrayrefvar.


The declaration of an array variable, and the creation of an array can be done with a single statement, as follows:

datatype[] Arrayrefvar = new Datatype[arraysize];


Alternatively, you can create an array using the following method.


Datatype[] Arrayrefvar = {value0, value1, ..., valuek};


The elements of an array are accessed through an index. The array index starts at 0, so the index value is from 0 to arrayrefvar.length-1.


Instance

The following statement first declares an array variable mylist, creates an array containing 10 elements of type double, and assigns its reference to the MyList variable.


double[] myList = new DOUBLE[10];

There are 10 double elements in the MyList array, and its subscript is from 0 to 9.


Array structure Description


Working with arrays

The element type and the size of the array are deterministic, so we usually use a basic loop or a foreach loop when working with array elements.


Instance

This example shows how to create, initialize, and manipulate an array in its entirety:

public class Testarray {

public static void Main (string[] args) {

Double[] MyList = {1.9, 2.9, 3.4, 3.5};


Print all array elements

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

System.out.println (Mylist[i] + "");

}

Calculates the sum of all elements

Double total = 0;

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

Total + = Mylist[i];

}

SYSTEM.OUT.PRINTLN ("total was" + total);

Find the largest element

Double max = mylist[0];

for (int i = 1; i < mylist.length; i++) {

if (Mylist[i] > max) max = mylist[i];

}

System.out.println ("Max is" + max);

}

}

The results of the above example compilation run as follows:

1.9

2.9

3.4

3.5

Total is 11.7

Max is 3.5


foreach Loop

JDK 1.5 introduces a new type of loop, called a foreach loop or a reinforced loop, that iterates through an array without using subscripts.


Instance

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

public class Testarray {

public static void Main (string[] args) {

Double[] MyList = {1.9, 2.9, 3.4, 3.5};


Print all array elements

for (double element:mylist) {

SYSTEM.OUT.PRINTLN (Element);

}

}

}

The results of the above example compilation run as follows:

1.9

2.9

3.4

3.5


Arrays as arguments to functions

An array can be passed as a parameter to a method. For example, the following example is a method for printing elements in an int array.


public static void PrintArray (int[] array) {

for (int i = 0; i < Array.Length; i++) {

System.out.print (Array[i] + "");

}

}


The following example calls the PrintArray method to print out 3,1,2,6,4 and 2:

PrintArray (New int[]{3, 1, 2, 6, 4, 2});


Array as the return value of the function

public static int[] Reverse (int[] list) {

Int[] result = new Int[list.length];


for (int i = 0, j = result.length-1; i < list.length; i++, j--) {

RESULT[J] = List[i];

}

return result;

}

The result array in the above instance is the return value of the function.



Arrays class

The Java.util.Arrays class makes it easy to manipulate an array, and all of the methods it provides are static. Has the following features:


Assigning a value to an array: through the Fill method.

Sort the array: By the Sort method, in ascending order.

Compare arrays: Compares the values of elements in an array by the Equals method.

Find array elements: The BinarySearch method can be used to perform binary lookups of sorted arrays.

Specify the following:


1. public static int BinarySearch (object[] A, Object key)

Searches for the object of the given value (byte,int,double, etc.) in the given array using a binary lookup algorithm. Arrays must be sorted before they are called. Returns the index of the search key if the lookup value is contained in the array, otherwise (-(insertion point)-1).


2. public static Boolean Equals (Long[] A, long[] A2)

Returns true if two specified long arrays are equal to each other. If two arrays contain the same number of elements, and all corresponding element pairs in the two arrays are equal, the two arrays are considered equal. In other words, if two arrays contain the same elements in the same order, the two arrays are equal. The same approach applies to all other basic data types (byte,short,int, etc.).


3. public static void Fill (int[] A, int val)

Assigns the specified int value to each element in the specified range in the specified array of type int. The same approach applies to all other basic data types (byte,short,int, etc.).


4. public static void sort (object[] a)

Sorts the specified array of objects in ascending order according to the natural ordering of their elements. The same approach applies to all other basic data types (byte,short,int, etc.).


The allocation is limited by the memory allocation, when the increment or decrement of the array, it is necessary to redistribute. Use System.arraycopy (THIS.A, 0, a, 0, size), get the original value, and then change the operation. The memory moves very much. ArrayList is a linear standard implemented on an array-based basis.


Data Structures-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.