Java--java Array

Source: Internet
Author: User

Arrays are one of the most important data structures for every editing language.

Declaring an array variable

Before you can use an array in your program, you must declare the array variable first. The following is the syntax for declaring an array variable:

Double[] myList;         // preferred method or double mylist[];         ///The same effect, but not the preferred method// recommend using datatype[] Arrayrefvar declaration style declares an array variable. DataType arrayrefvar[] style is from the C + + language and is used in Java in order to enable C + + programmers to quickly understand the Java language. 
Create an array
New Datatype[arraysize];

The above syntax statement does two things:

    1. An array was created using datatype[arraysize].
    2. Assigns a reference to the newly created array to the variable Arrayrefvar.
declaring + Creating an array
New datatype[arraysize]; or datatype[] Arrayrefvar = {value0, value1, ..., valuek};
Instance:
PublicClassTestarray {PublicStaticvoidMain (string[] args) {Double[] MyList = {1.9, 2.9, 3.4, 3.5};//Print all array elementsfor (int i = 0; i < mylist.length; i++) {System.out.println (Mylist[i] + ""); }//computes the sum of all elements of double total = 0; For (int i = 0; i < mylist.length; i++) {total + = mylist[i];} SYSTEM.OUT.PRINTLN ("total was" + total ); // Find maximum 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);}}             
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.

Example
This instance is used to display all elements in the array myList:class Testarray {   voiddouble[] myList = {1.9, 2.9, 3.4, 3.5//  For (double element:mylist) {System.out.println (element);}}}  /* Output: 1.92.93.43.5* /         
Arrays as arguments to functions
void PrintArray (int[] array) {for  (int i = 0; i < array.length; i++) {System.out.print (array[ I] + "");}}     
Array as the return value of the function
int[] Reverse (int[] list) {  intfor (int i = 0, j = result.length-1; i < list.length; i++, J- -) {Result[j] =return result;}       
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.

Please refer to the table below for specific instructions:

Serial Number Methods and Instructions
1 public static int BinarySearch (object[] A, object key) uses the binary lookup algorithm to search the given array for objects of the given value (byte,int,double, etc.). 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 their natural sequence of elements. The same approach applies to all other basic data types (byte,short,int, etc.).

Java--java Array

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.