Java Data Structures and algorithms (chapter II arrays)

Source: Internet
Author: User

Arrays are the most widely used data storage structures. It is implanted in most programming languages.

Basic knowledge of arrays in Java

Create an array

They are treated as objects in Java, so you must use the new operator to create an array:

Int[] Intarray;    Defines a reference to an array Ingarray = new int[100];  Creates the array, and//sets int array to refer to it//or using equivalent single statement declaration method: int[] int Array = new INT[100];

An array is an object, so its name (Intarray in the previous program) is a reference to the array; The array is stored in other addresses in memory, and Intarray only holds the address. The array has a length field that lets you know the current array size (number of data items);

int arraylength = Intarray.length; Find array size

Once an array is created, the stool and urine cannot be changed.

Accessing array data items

Array data items are accessed by subscript in square brackets.

temp = intarray[3];            Get contents of fourth element of array intarray[7] = 66; Intsert to the Eighth cell

Initialization

When an integer array is created, the array is automatically initialized to null without specifying it.

Unless specific values are assigned to the array's data items, they are always special null objects.

Use the following syntax to initialize an array of a primitive type:

Int[] Intarray = {1,3,45,23,123,122,56};

The size of the array is determined by the data items in the data list, and the fixed size is immutable;

Java code for an ordered array

The following is a discussion of the Java code for an ordered array, which uses the Ordarray class to encapsulate the array and its algorithm. The core of the class is the Find () method, which is used to locate a particular data item by means of a binary lookup class.

Summary

    • An array in Java is an object created by the new operator;

    • Unordered arrays can provide fast insertions, but lookups and deletions are slower;

    • Encapsulating an array into a class can protect the array from arbitrary changes;

    • A method (and sometimes a field) that a user can access to a class's interface class;

    • The interface of the class is designed to make the operation of the class user more simple;

    • An ordered array can be found using two points;

    • The logarithm of base a of B (probably) is the number of times the B is removed before the result of drizzle 1;

    • The time required for linear lookups is proportional to the number of data items in the array;

    • The time required for binary lookup is proportional to the logarithm of the number of data items in the array;

    • The large O notation provides a convenient method for comparing the speed of the algorithm.

    • O (1) time algorithm is the best, O (logn) Second, O (N) for the General, O (N2) the worst;







Page 36

Java Data Structures and algorithms (chapter II arrays)

Related Article

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.