Java data structure and algorithm 2

Source: Internet
Author: User
The linear storage structure of the array is disordered and ordered. The search, insert, and delete algorithms must consider whether there are repeated elements in the array. In Java, arrays in Basic Java are used as objects, so new is used to generate objects. Int ar [] = new int [10]; array value: [] Operator, subscript index starts from 0 to length-1; array initialization: if the object is assigned a value, the default value is null, if this value is used, null pointer exception is returned. Int ar [] = new int [] {,}; int ar [] = {,}; (initialization list) // array. Java
// Demonstrates Java Arrays
// To run this program: C> JAVA arrayapp
Import java. Io. *; // For I/O
//////////////////////////////////////// ////////////////////////
Class arrayapp
{
Public static void main (string [] ARGs) throws ioexception
{
Int [] arr; // reference
Arr = new int [100]; // make Array
Int nelems = 0; // number of items
Int J; // loop counter
Int searchkey; // key of item to search //-------------------------------------------------------------
-
Arr [0] = 77; // Insert 10 items
Arr [1] = 99;
Arr [2] = 44;
Arr [3] = 55;
Arr [4] = 22;
Arr [5] = 88;
Arr [6] = 11;
Arr [7] = 00;
Arr [8] = 66;
Arr [9] = 33;
NElems = 10; // now 10 items in array //-------------------------------------------------------------
-
For (j = 0; j <nElems; j ++) // display items
System. out. print (arr [j] + "");
System. out. println ("");
//-------------------------------------------------------------
-
SearchKey = 66; // find item with key 66
For (j = 0; j <nElems; j ++) // for each element,
If (arr [j] = searchKey) // found item?
Break; // yes, exit before end
If (j = nElems) // at the end?
System. out. println ("Can't find" + searchKey); // yes
Else
System. out. println ("Found" + searchKey); // no
//--------------------------------------------------------------
SearchKey = 55; // delete item with key 55
For (j = 0; j <nElems; j ++) // look for it
If (arr [j] = searchKey)
Break;
For (int k = j; k <nElems; k ++) // move higher ones
Down
Arr [k] = arr [k + 1];
NElems --; // decrement size
//-------------------------------------------------------------
-
For (j = 0; j <nElems; j ++) // display items
System. out. print (arr [j] + "");
System. out. println ("");
} // End main ()
} // End class ArrayApp and above are added and deleted by procedural search. The object-oriented system first separates the data storage structure and data users, so as to clarify the functions of the program and make it easy to understand the design and maintenance. // LowArray. java
// Demonstrates array class with low-level interface
// To run this program: C> java LowArrayApp
Import java. io. *; // for I/O
//////////////////////////////////////// ////////////////////////
Class LowArray
{
Private double [] a; // ref to array
Public LowArray (int size) // constructor
{
A = new double [size];
}
// Put element into array
Public void setElem (int index, double value)
{
A [index] = value;
}
Public double getElem (int index) // get element from array
{
Return a [index];
}
} // End class LowArray
//////////////////////////////////////// ////////////////////////
Class LowArrayApp
{
Public static void main (String [] args)
{
LowArray arr; // reference
Arr = new lowarray( 100); // create LowArray object
Int nElems = 0; // number of items in array
Int j; // loop variable
Arr. setElem (0, 77); // insert 10 items
Arr. setElem (1, 99); arr. setElem (2, 44 );
Arr. setElem (3, 55 );
Arr. setElem (4, 22 );
Arr. setElem (5, 88 );
Arr. setElem (6, 11 );
Arr. setElem (7, 00 );
Arr. setElem (8, 66 );
Arr. setElem (9, 33 );
NElems = 10; // now 10 items in array
//-------------------------------------------------------------
-
For (j = 0; j <nElems; j ++) // display items
System. out. print (arr. getElem (j) + "");
System. out. println ("");
//-------------------------------------------------------------//-------------------------------------------------------------
-
Int searchkey = 26; // search for data item
For (j = 0; j <nelems; j ++) // for each element,
If (ARR. getelem (j) = searchkey) // found item?
Break;
If (j = nelems) // No
System. Out. println ("can't find" + searchkey );
Else // Yes
System. Out. println ("found" + searchkey );
//-------------------------------------------------------------
-
// Delete value 55
For (j = 0; j <nelems; j ++) // look for it
If (ARR. getelem (j) = 55)
Break;
For (int K = J; k <nelems; k ++) // move higher ones
Down
Arr. setelem (K, arr. getelem (k + 1 ));
NElems --; // decrement size
//-------------------------------------------------------------
-
For (j = 0; j <nElems; j ++) // display items
System. out. print (arr. getElem (j) + "");
System. out. println ("");
} // End main ()
} // End class LowArrayApp is a simple encapsulation of classes. Classes used to store data are called containers. They not only store data, but also provide complex operations such as access methods and sorting methods. Interface: It generally refers to the declaration of methods in the class. The encapsulation of the above classes is not practical, because the setElem and getElem methods still execute the [] operation and are still at a lower level, just changed the form. // highArray. java
// Demonstrates array class with high-level interface
// To run this program: C> java HighArrayApp
Import java. io. *; // for I/O
//////////////////////////////////////// ////////////////////////
Class HighArray
{
Private double [] a; // ref to array
Private int nElems; // number of data items
//----------------------------------------------------------
-
Public HighArray (int max) // constructor
{
A = new double [max]; // create the array
NElems = 0; // no items yet
}
//----------------------------------------------------------
-
Public boolean find (double searchKey)
{// Find specified value
Int j;
For (j = 0; j <nElems; j ++) // for each element,
If (a [j] = searchKey) // found item?
Break; // exit loop before end
If (j = nElems) // gone to end?
Return false; // yes, can't find it
Else
Return true; // no, found it
} // End find ()
//----------------------------------------------------------
-
Public void insert (double value) // put element into Array
{
A [nelems] = value; // insert it
-43-
Nelems ++; // increment size
}
//----------------------------------------------------------
-
Public Boolean Delete (double value)
{
Int J;
For (j = 0; j <nelems; j ++) // look for it
If (value = A [J])
Break;
If (j = nelems) // can't find it
Return false;
Else // found it
{
For (int K = J; k <nelems; k ++) // move higher ones down
A [k] = A [k + 1];
Nelems --; // decrement size
Return true;
}
} // End Delete ()
//----------------------------------------------------------
-
Public void display () // displays array contents
{
For (int j = 0; j <nElems; j ++) // for each element,
System. out. print (a [j] + ""); // display it
System. out. println ("");
}
//----------------------------------------------------------
-
} // End class HighArray
//////////////////////////////////////// ////////////////////////
Class HighArrayApp
{
Public static void main (String [] args)
{
Int maxSize = 100; // array size
HighArray arr; // reference to array
Arr = new HighArray (maxSize); // create the array
Arr. insert (77); // insert 10 items
Arr. insert (99 );
Arr. insert (44 );
Arr. insert (55 );
Arr. insert (22 );
Arr. insert (88 );
Arr. insert (11 );
Arr. insert (00 );
-44-
Arr. insert (66 );
Arr. insert (33 );
Arr. display (); // display items
Int searchKey = 35; // search for item
If (arr. find (searchKey ))
System. out. println ("Found" + searchKey );
Else
System. out. println ("Can't find" + searchKey );
Arr. delete (00); // delete 3 items
Arr. delete (55 );
Arr. delete (99 );
Arr. display (); // display items again
} // End main ()
} // End class HighArrayApp abstracts the above and separates "how to do" from "what is" to become an abstraction. (How to execute operations in a class and do not need to be exposed to the class users) Why do ordered Groups use ordered groups? The binary method increases the search speed. The following search methods can be used for Ordered Groups: Linear and binary. Linear search: The Search Method in an ordered group is the same as that in an unordered group. You can complete the search by finding one. Binary Search: public int find (double searchKey)
{
Int lowerBound = 0;
Int upperBound = nElems-1;
Int curIn;
While (true)
{
CurIn = (lowerBound + upperBound)/2;
If (a [curIn] = searchKey)
Return curIn; // found it
Else if (lowerBound> upperBound)
Return nElems; // can't find it
Else // divide range
{
If (a [curIn] <searchKey)
LowerBound = curIn + 1; // it's in upper half
Else
UpperBound = curIn-1; // it's in lower half
} // End else divide range
} // End while
} // End find () // orderedArray. java
// Demonstrates ordered array class
// To run this program: C> java OrderedApp
Import java. io. *; // for I/O
//////////////////////////////////////// ////////////////////////
Class OrdArray
{
Private double [] a; // ref to array
Private int nElems; // number of data items
//----------------------------------------------------------
-
Public OrdArray (int max) // constructor
{
A = new double [max]; // create array
NElems = 0;
}
//----------------------------------------------------------
-
Public int size ()
{Return nElems ;}
//----------------------------------------------------------
-
Public int find (double searchKey)
{
Int lowerBound = 0;
Int upperBound = nElems-1;
Int curIn;
While (true)
{
CurIn = (lowerBound + upperBound)/2;
If (a [curIn] = searchKey)
Return curIn; // found it else
If (lowerBound> upperBound)
Return nElems; // can't find it
Else // divide range
{
If (a [curIn] <searchKey)
LowerBound = curIn + 1; // it's in upper half
Else
UpperBound = curIn-1; // it's in lower half
} // End else divide range
} // End while
-51-
} // End find ()
//----------------------------------------------------------
-
Public void insert (double value) // put element into array
{
Int j;
For (j = 0; j <nElems; j ++) // find where it goes
If (a [j]> value) // (linear search)
Break;
For (int k = nElems; k> j; k --) // move higher ones up
A [k] = a [k-1];
A [j] = value; // insert it
NElems ++; // increment size
} // End insert ()
//----------------------------------------------------------
-
Public boolean delete (double value)
{
Int j = find (value );
If (j = nElems) // can't find it
Return false;
Else // found it
{
For (int k = j; k <nElems; k ++) // move higher ones down
A [k] = a [k + 1];
NElems --; // decrement size
Return true;
}
} // End delete ()
//----------------------------------------------------------
-
Public void display () // displays array contents
{
For (int j = 0; j <nElems; j ++) // for each element,
System. out. print (a [j] + ""); // display it
System. out. println ("");
}
//----------------------------------------------------------
-
} // End class OrdArray
//////////////////////////////////////// ////////////////////////
Class OrderedApp
{
Public static void main (String [] args)
{
Int maxSize = 100; // array size
OrdArray arr; // reference to array
Arr = new OrdArray (maxSize); // create the array
-52-
Arr. insert (77); // insert 10 items
Arr. insert (99 );
Arr. insert (44 );
Arr. insert (55 );
Arr. insert (22 );
Arr. insert (88 );
Arr. insert (11 );
Arr. insert (00 );
Arr. insert (66 );
Arr. insert (33 );
Int searchKey = 55; // search for item
If (arr. find (searchKey )! = Arr. size ())
System. out. println ("Found" + searchKey );
Else
System. out. println ("Can't find" + searchKey );
Arr. display (); // display items
Arr. delete (00); // delete 3 items
Arr. delete (55 );
Arr. delete (99 );
Arr. display (); // display items again
} // End main ()
} // Advantages of the end class OrderedApp ordered group: Fast Searching and slow insertion/deletion. It is often used to find and delete data that is not often inserted or deleted. Storage object class Person
{
Private String lastName;
Private String firstName;
Private int age;
//----------------------------------------------------------
-
Public Person (String last, String first, int a) {// constructor
-56-
LastName = last;
FirstName = first;
Age =;
}
//----------------------------------------------------------
-
Public void displayPerson ()
{
System. out. print ("Last name:" + lastName );
System. out. print (", First name:" + firstName );
System. out. println (", Age:" + age );
}
//----------------------------------------------------------
-
Public String getLast () // get last name
{Return lastName ;}
} // End class Person class ClassDataArray
{
Private Person [] a; // reference to array
Private int nElems; // number of data items
//----------------------------------------------------------
-
Public ClassDataArray (int max) // constructor
{
A = new Person [max]; // create the array
NElems = 0; // no items yet
}
//----------------------------------------------------------
-
Public Person find (String searchName)
{// Find specified value
Int j;
For (j = 0; j <nElems; j ++) // for each element,
If (a [j]. getLast (). equals (searchName) // found
Item?
Break; // exit loop before
End
If (j = nElems) // gone to end?
Return null; // yes, can't find it
-58-
Else
Return a [j]; // no, found it
} // End find ()
//----------------------------------------------------------
-
// Put Person into array
Public void insert (String last, String first, int age)
{
A [nElems] = new Person (last, first, age );
NElems ++; // increment size
}
//----------------------------------------------------------
-
Public boolean delete (String searchName)
{// Delete Person from
Array
Int j;
For (j = 0; j <nElems; j ++) // look for it
If (a [j]. getLast (). equals (searchName ))
Break;
If (j = nElems) // can't find it
Return false;
Else // found it
{
For (int k = j; k <nElems; k ++) // shift down
A [k] = a [k + 1];
Nelems --; // decrement size
Return true;
}
} // End Delete ()
//----------------------------------------------------------
-
Public void displaya () // displays array Contents
{
For (Int J = 0; j <nelems; j ++) // for each element,
A [J]. displayperson (); // display it
}
//----------------------------------------------------------
-
} // End class classdataarray class classdataapp
{
Public static void main (string [] ARGs)
{
Int maxsize = 100; // array size
Classdataarray arr; // reference to array
Arr = new classdataarray (maxsize); // create the Array
-59-
// Insert 10 items
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 ("creden"", "Lucinda", 18 );
Arr. displayA (); // display items
String searchKey = "Stimson"; // search for item
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 ("deleting Smith, Yee, and creden ");
Arr. Delete ("Smith"); // delete 3 items
Arr. Delete ("yee ");
Arr. Delete ("creden ");
Arr. displaya (); // display items again
} // End main ()
} // End class classdataapp
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.