Java array and data structure

zhaozj2021-02-16  118

Java array and data structure

In the Java programming language, the array is taken as an object, so the New operator must be used when creating an array.

Int [] INTARRAY = New Int [100];

[] Operators are a flag for the compiler, which shows a array object that is being named instead of an ordinary variable.

Since the array is an object, its name is a reference to an array; it is not an array itself. Arranges are stored in other addresses in memory, and Intarray only saves this address.

An array has a Length field, which can learn the size of the current array:

INT Arraylength = INTARRAY.LENGTH;

Like most programming languages, once an array is created, the array size cannot be changed.

The most basic operation in the array is insert, lookup, delete.

Public class higharray {

Private long [] a;

Private Int Nlems;

/ ** Creates a new installation of higharray * /

Public higharray (int max) {

a = new long [max];

NELEMS = 0;

}

// ............................................ ...........

Public Boolean Find (Long SearchKey)

{

Int J;

For (j = 0; j

IF (A [J] == SearchKey)

Break;

IF (j == nlems)

Return False;

Else

Return True;

} // end find ()

// ............................................ ...........

Public void insert (long value) {

A [Nlems] = Value;

NELEMS ;

}

// ............................................ ...........

Public Boolean Delete (Long Value)

{

Int J;

For (j = 0; j

IF (value == a [j])

Break;

IF (j == nlems)

Return False;

Else

{

For (int K = J; K

a [k] = a [k 1];

NELEMS -

Return True;

}

} // end delete ()

// ............................................ ...........

Public void display ()

{

For (int J = 0; J

System.out.print (a [j] ");

System.out.println ("");

} // end display ()

}

Assume an array, where the data items are arranged in a keyword or descending order, called an ordered array. The advantage of sorting arrays can significantly improve lookup speeds by two-point lookup. Wood is inserted in insertion operations due to all releared data to make space, so speed is slow. The delete operation in the ordered array and the unordered array is very slow. This is because the data item must move forward to fill the hole that has been deleted.

Ordered arrays are very useful in situations where it is frequent, but if it is more frequent insertion and deletion, it cannot work efficiently. Public class ordarray {

Private long [] a;

Private Int Nlems;

/ ** Creates a new instance of oordaray * /

Public Ordaray (int max) {

a = new long [max];

NELEMS = 0;

}

Public int size () {

Return NELEMS;

}

Public int find (long searchkey) {

INT LOWERBOUND = 0;

INT UPPERBOUND = NELEMS-1;

Int curin;

While (true) {

Curin = (LowerBound UpperBound) / 2;

IF (a [curin] == searchkey)

Return Curin;

Else IF (LowerBound> UpperBound)

Return NELEMS;

Else

{

IF (a [curin]

LowerBound = Curin 1;

Else

Upperbound = curin-1;

}

}

}

Public void insert (long value) {

Int J;

For (j = 0; j

IF (A [J]> Value)

Break;

For (int K = NELEMS; K> J; K -)

a [k] = a [k-1];

A [J] = Value;

NELEMS ;

}

Public Boolean Delete (Long Value) {

INT J = Find (Value);

IF (j == nlems)

Return False;

Else

{

For (int K = J; K

a [k] = a [k 1];

NELEMS -

Return True;

}

}

Public void display ()

{

For (int J = 0; J

System.out.print (a [j] ");

System.out.println ("");

}

}

Storage object

Usually we store data records are a collection of many fields. A data record in Java is often represented by a class object. For example, a typical class can be constructed to store a company's staff data.

Public class person {

PRIVATE STRING LASTNAME;

Private string dimstname;

PRIVATE INT AGE;

/ ** Creates a new instance of persourn * /

Void Person (String Last, String First, INT A) {

Lastname = last;

Firstname = first;

AGE = a;

}

Void DisplayPerson () {

System.out.print ("Last Name: lastname);

System.out.print (", FirstName:" firstname);

System.out.print (", AGE:" AGE);

}

Public string getLast () {

Return Lastname;

}

}

The constructor creates a new Person object and initializes its fields. The DisplayPerson method shows the data of a Person object. GetLast returns the last name of Person; this is the keyword field used to search for. Public class person {

PRIVATE STRING LASTNAME;

Private string dimstname;

PRIVATE INT AGE;

/ ** Creates a new instance of persourn * /

Public Person (String Last, String First, Int A) {

Lastname = last;

Firstname = first;

AGE = a;

}

Public void displayperson () {

System.out.print ("Last Name: lastname);

System.out.print (", FirstName:" firstname);

System.out.println (", AGE:" AGE);

}

Public string getLast () {

Return Lastname;

}

}

Public class classdataapp {

/ **

* @Param Args the Command Line Arguments

* /

Public static void main (String [] args) {

INT maxSize = 100;

ClassDataArray Arr;

Arr = new classdataArray (maxsize);

Arr.insert ("Evans", "Patty", 24);

Arr.insert ("Smith", "Lorraine", 37);

Arr.insert ("Yee", "Tom", 43);

Arr.insert ("Adams", "Henry", 63);

Arr.insert ("Hashimoto", "SATO", 21);

Arr.insert ("Stimson", "Henry", 29);

Arr.insert ("Velasquez", "Jose", 72);

Arr.insert ("Lamarque", "Henry", 54);

Arr.insert ("VANG", "Minh", 22);

Arr.insert ("CRESWell", "Lucinda", 18);

Arr.displaya ();

String searchkey = "stimson";

Person Found;

Found = arr.find (searchkey);

IF (Found! = NULL)

{

System.out.print ("Found");

Found.displayPerson ();

}

Else

System.out.println ("Can't Find" SearchKey;

System.out.Println ("deleding smith, yee, and creswell");

Arr.delete ("smith");

Arr.delete ("yee");

Arr.delete ("CRESwell");

Arr.displaya ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-10281.html

New Post(0)