Java Array Implementation iteration method iterator, imitating arraylist

Source: Internet
Author: User

I searched the internet and found no articles about array iteration. So I wrote it myself.

It is mainly used to simulate an arraylist implementation!

The Code is as follows:

First, create an interface:

package com.list;public interface List<T> extends Iterable<T>{ void add(T e); void remove(T t); void removeAt(int index); int size(); void clear(); int indexOf(Object object); void set(T e,int index);}

The second part sets up the arraylist class:

package com.list.impl;import java.util.Iterator;import com.list.List;public class ArrayList<T> implements List<T> {Object [] objects=new Object[0];public void add(T t) {// TODO Auto-generated method stubObject [] tmp=new Object[objects.length+1];tmp[tmp.length-1]=t;System.arraycopy(objects, 0, tmp, 0, objects.length);objects=tmp;}@Overridepublic void remove(T t) {// TODO Auto-generated method stubfor(int i=0;i<objects.length;i++){if(objects[i].equals(t)){if(i>0){Object object[]=new Object[objects.length-1];System.arraycopy(objects, 0, object, 0, i);System.arraycopy(objects, i+1, object, i, objects.length-i-1);objects=object;break;}}}}@Overridepublic void removeAt(int index) {// TODO Auto-generated method stubif(index>=0){Object object[]=new Object[objects.length-1];System.arraycopy(objects, 0, object, 0, index);System.arraycopy(objects, index+1, object, index, objects.length-index-1);objects=object;}}@Overridepublic int size() {// TODO Auto-generated method stubreturn objects.length;}@Overridepublic void clear() {// TODO Auto-generated method stubthis.objects=new Object[0];}@Overridepublic int indexOf(Object object) {// TODO Auto-generated method stubfor(int i=0;i<objects.length;i++){if(objects[i].equals(object)){return i;}}return -1;}@Overridepublic void set(T e, int index) {// TODO Auto-generated method stubobjects[index]=e;}@Overridepublic Iterator<T> iterator() {// TODO Auto-generated method stubIterator<T> iterator=new Iterator<T>() {int index=0;boolean hasNext=true;@Overridepublic boolean hasNext() {// TODO Auto-generated method stubreturn hasNext;}@Overridepublic T next() {// TODO Auto-generated method stubif(index++<objects.length-1){hasNext=true;return (T)objects[index-1];}else {hasNext=false;return (T)objects[objects.length-1];}}@Overridepublic void remove() {// TODO Auto-generated method stubobjects=new Object[0];}};return iterator;}}

Arraylist needs to implement the iterator Interface

This interface has these methods
Hasnext ()
Next ()
Remove ()

Does hasnext have the next data?

Next get the next object

Remove remove all

After implementing the iterator interface in the class, rewrite the public iterator <t> iterator () method.

Iterator <t> iterator = new iterator <t> () {int Index = 0; Boolean hasnext = true; @ overridepublic Boolean hasnext () {// todo auto-generated method stubreturn hasnext;} @ overridepublic T next () {// todo auto-generated method stubif (index ++ <objects. length-1) {// determine whether the array hasnext = true; Return (t) objects [index-1];} else {hasnext = false; Return (t) objects [objects. length-1]; // convert the array object to a generic type} @ overridepublic void remove () {// todo auto-generated method stubobjects = new object [0]; // This is an array object }}; return iterator;

In this way, a simple arraylist is implemented. You can view the JDK source code, and the implementation method is similar.

The removal method is also used in the official JDK source code.

System.arraycopy(objects, 0, object, 0, index);

How to copy an array!

The following is my use and test code:

Package COM. test; import COM. list. *; import COM. list. impl. arraylist; public class test {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stublist <Object> List = new arraylist <Object> (); string STR = "Zhang San"; List. add (1); list. add (2); list. add (3); list. add (new object (); list. add (new object (); list. add (new object (); list. add (STR); list. add ("hello"); list. remove (STR); list. remove (STR); list. removeat (0); For (Object object: List) {system. out. println (object);} system. out. println (list. size ());}}

We can see that the method of use is the same as that of the official website!

Looking at the JDK source code is rewarding !~

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.