Self-implemented HashSet class and rrayList class

Source: Internet
Author: User

I implemented the HashSet class and ArrayList class myself, but simply implemented several of their methods, and the writing class was very restrictive. When using the classes provided by JDK, you should know how their underlying details are implemented, which will be helpful for future use and addition. The JDK document provides a method description, without the details of the underlying implementation, and without a simple example of each method implementation.
Check the Code:
Self-written HashSet class
[Java]
Public class MyHashSet {

Static int num = 0;
Static Object [] object;
Public MyHashSet () // No-parameter Constructor
{
Object = new Object [10];
}
Public MyHashSet (int number) // constructor with Parameters
{
Object = new Object [number];
}
Private static void Add (Object E) // Add Element Method
{
Object [num ++] = E;
}
Private static boolean MyContains (Object E) // check whether an element method exists
{
For (int I = 0; I <num; I ++)
{
If (object [I] = E) return true;
}
Return false;
}
Private static void ForEach () // Traversal method
{
For (int I = 0; I <num; I ++)
{
System. out. print (object [I] + "");
}
System. out. println ();
}
Private int Size () // returns the Size of the hashset, that is, the number of elements.
{
Return num;
}

Public static void main (String [] args ){
MyHashSet myhashset = new MyHashSet (10 );
// Add Method
For (int I = 1; I <= 10; I ++)
{
MyHashSet. Add (I );
}
// Traversal method
System. out. println ("myhashset element :");
MyHashSet. ForEach ();

// Number of elements
Int size = myhashset. Size ();
System. out. println ("the number of myhashset elements is" + size );

// Check whether an element exists
Int checknum1 = 4;
If (MyContains (checknum1) = true)
{
System. out. println ("elements in myhashset" + checknum1 );
}
Else
{
System. out. println ("the element does not exist in myhashset" + checknum1 );
}
}
}

ArrayList class:
[Java]
Public class MyArrayList {
Static int num = 0;
Static Object [] object;
Public MyArrayList ()
{
Object = new Object [10];
}
Public MyArrayList (int number)
{
Object = new Object [number];
}
Private void Add (Object E)
{
Object [num ++] = E;
}
Private static boolean MyContain (Object E)
{
For (int I = 0; I <num; I ++)
{
If (object [I] = E)
Return true;
}
Return false;
}
Private static void ForEach ()
{
For (int I = 0; I <num; I ++)
{
System. out. print (object [I] + "");
}
System. out. println ();
}
Private Object ReMove (Object E)
{
Int I = 0;
While (object [I]! = E) I ++;
For (; I <num; I ++)
{
Object [I] = object [I + 1];
}
Num = num-1;
Return object;
}
Public static void main (String [] args ){

// Statement
MyArrayList myArrayList = new MyArrayList ();
// Add Method
For (int I = 1; I <10; I ++)
{
MyArrayList. Add (I );
}
// Traversal method
System. out. println ("myArrayList element :");
MyArrayList. ForEach ();

// Check whether an element exists
Int checknum1 = 4;
If (MyContain (checknum1) = true)
{
System. out. println ("elements in myArrayList" + checknum1 );
}
Else
{
System. out. println ("elements not in myArrayList" + checknum1 );
}
// Remove element 1
Int checknum2 = 1;
MyArrayList. ReMove (checknum2 );
System. out. println ("Remove element" + checknum2 + :");
MyArrayList. ForEach ();
}
}

 

 

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.