Demonstrate the necessity of data type packaging and generic classes

Source: Internet
Author: User
Tags comparable

* Demonstrate the necessity of data type packaging and generic classes
Package seek;

* Design a sorted object array class to implement the search algorithm:
* This example demonstrates the use of the comparable interface and the classes that implement this interface, and the necessity of data type packaging and generic classes.
* In this example, you want to design a sortedarray array class of sorted objects. This class can store multiple objects of a class in sequence in an array.
* The class to which an array element belongs. If both integer and string classes are supported, the function of searching for a specified object in an array can be implemented.
Public class sortedarray <t> {
Private comparable table []; // storage implementation comparable interface object
Private int count;
Public sortedarray (int n)
If (n <= 0) n = 10;
This. Table = new comparable [N];
This. Count = 0;
Public void print () // output array element
System. Out. Print ("table :");
For (INT I = 0; I <this. Count; I ++)
System. Out. Print ("" + Table [I]. tostring ());
System. Out. println ();
Public int search (T tobj) // sequential search
Int I = 0;
While (I <this. Count &&! This. Table [I]. Equals (tobj) // compare whether two objects are equal
I ++;
If (I <this. Count) // search successful
Return I + 1; // convert subscript to serial number
Return-1; // search failed
Public Boolean insert (T tobj) // Insert elements into the array in ascending order
If (this. Count <this. Table. Length & tobj instanceof comparable) // when the array is not full and tobj is a comparable interface object
Int I = 0;
Comparable cmpobj = (comparable) tobj;
While (I <this. Count & cmpobj. compareto (this. Table [I])> = 0) // compare the size of the two objects
I ++;
For (Int J = This. Count-1; j> = I; j --) // move several elements backward.
Table [J + 1] = table [J];
This. Table [I] = cmpobj; // insert
This. Count ++;
Return true; // insert successful
Return false; // insertion failed

Package study;
Import seek. sortedarray;
Class sortedarray_ex {
Public static void main (string [] ARGs ){
Sortedarray <integer> sal1 = new sortedarray <integer> (10 );
Integer required bj = new INTEGER (2 );
Sal1.insert (distinct BJ );
Required bj = new INTEGER (3 );
Sal1.insert (distinct BJ );
Required bj = new INTEGER (1 );
Sal1.insert (distinct BJ );
Sal1.print ();

Sortedarray <string> SA2 = new sortedarray <string> (10 );
String strobj = new string ("XYZ ");
Sa2.insert (strobj );
Strobj = new string ("AAA ");
Sa2.insert (strobj );
Strobj = new string ("ABC ");
Sa2.insert (strobj );
Sa2.print ();
System. Out. println ("Search" + strobj. tostring () + ":" + (strobj ));



] Program design instructions:

1> Universal Design of array elements:
To design a common object array that has operations (such as sequential storage or search) for multiple classes, there may be multiple classes to which the array elements belong, it cannot be determined during compilation and must be determined during runtime. The common practice is to declare an array element as an object class, for example:
Private object table [];
According to the principle of real-time parent class Object of the subclass object, when running the program, the array element table [I] can be assigned a value of any type.
The preceding statement does not work for this example. Because the array element table [I] is an object class object, it can only call the object class method, and the object class only has the equals () method, without compareto () comparison Method, whether the comparison between objects is equal, the size of values cannot be compared, so the sorting function cannot be implemented. Therefore, this example declares that the array element type is comparable, which can be copied as an object of the class implementing the comparable interface. The statement is as follows:
Private comparable table []; // storage implementation comparable interface object

2> necessity of using integer objects:
In this example, you want the array element, integer, or character set. For the purpose of universality of the program, the type of the array element is declared as comparable. The array can only store objects, but not integer values, therefore, the integer value must be constructed into an integer object to store the array, which reflects the significance of Java Declaration of the basic data type packaging class.

3> Differences between the use of generic classes or not:
In this example, if you want an array to store multiple objects of the same class, or integer objects or string objects, you must compare the objects of the same class to implement search and sorting algorithms, the comparison between objects of different classes has no practical significance.
Both the integer and string classes implement the compareto () method in the comparable interface. The methods of the two classes are declared as follows:
Public int compareto (integer anotherinteger) // integer class
Public int compareto (string anotherstring) // string class
Therefore, comparing objects of different classes produces a runtime error.
If the sortedarray class is not declared as a generic class, for example:
Public class sortedarray
Then, sortedarray Class Object SA1 can be mixed to store integer and string classes that implement the comparable interface. However, when an object is inserted and the comparato () method is used to compare the integer and string objects, an error occurs during the runtime.
After the sortedarray class is declared as a generic class, when creating an object, you must determine that the parameter of this class is another class, such:
Sortedarray <integer> SA1 = new sortedarray <integer> (10 );
At this time, integer objects are inserted in SA1. If a String object is inserted, an error occurs during compilation.

Therefore, for this example, declaring a generic class ensures that all elements in an array are objects of the same class. In addition, the generic class is used to advance the runtime type check to the compilation, making the code safer.

I'm exhausted!

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: 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.