Java Learning Note-The underlying implementation of the simulation ArrayList container

Source: Internet
Author: User

Package myarraylist;
Import java.util.*;
/**
* Simulate the implementation of the ArrayList class in the JDK
* @author Iwang
*
*/
public class myArrayList {
/**
* The value is used for Object storage.
*/
Private object[] value;

/**
* The size is the number of Object used.
*/
private int size;

public void Rangecheck () {
try {
throw new Exception ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

Public Object Set (int index,object obj) {
Rangecheck ();
Object old = Value[index];
Value[index] = obj;
return old;
}

public int indexOf (Object obj) {
if (obj==null) {
return-1;
}else{
for (int i=0;i<value.length;i++) {
if (Obj==value[i]) {
return i;
}
}
return-1;
}
}

public int lastIndexOf (Object obj) {
if (obj==null) {
return-1;
}else{
for (int i=value.length-1;i>=0;i--) {
if (Obj==value[i]) {
return i;
}
}
return-1;
}
}

public int size () {
return size;
}
public Boolean isEmpty () {
return size = = 0;
}

Public myArrayList () {
Value = new OBJECT[10];
This (10);
}
public myarraylist (int size) {
if (size<0) {
Rangecheck ();
}
Value = new Object[size];
}
public void InPut (Object obj) {
Value[size] = obj;
size++;
if (size>value.length-1) {
int capacity = (value.length << 1) + 2;
object[] NewList = new Object[capacity];
for (int i=0;i<value.length;i++) {
Newlist[i] = Value[i];
}
value = NewList;
}
}
Public Object getvalue (int index) {
if (index<0| | INDEX&GT;SIZE-1) {
Rangecheck ();
}
return Value[index];
}
public static void Main (string[] args) {
myArrayList a = new myarraylist (2);
A.input ("ABCD");
A.input (New Human ("is ME");

A.input ("123456");
A.input ("123456");
A.input ("123456");
A.input ("123456");
A.input ("123456");

Human me = (Human) a.getvalue (1);
System.out.println (Me.getname ());

System.out.println (A.size ());

System.out.println (A.indexof ("ABCD"));
System.out.println (A.lastindexof ("ABCD"));

}
}

Package myarraylist;

public class Human {
private String name;
Public Human (String name) {
THIS.name = name;
}
Public String GetName () {
return this.name;
}
public void SetName (String name) {
THIS.name = name;
}
}

Java Learning Note-The underlying implementation of the simulation ArrayList container

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.