Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of iterator in java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to achieve the iterator function in java", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve iterator function in java" article.

Two lines of code

List list=new ArrayList (2); list.add ("java"); list.add ("C #"); Iterator iterator=list.iterator (); while (iterator.hasNext ()) {System.out.println (iterator.next ());}

The above code is an iterative feature that is common in java.

So I also want to write a generic class and then support this iterative function.

So I wrote a dynamic array function similar to ArrayList.

Package a public import javax.swing.text.html.HTMLDocument;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;public class Gys {private final static int default_capacity = 10; private int endIndex = 0; private Object [] elemts; public Gys () {this.elemts = new object [default _ object];} public T [] add (T t) {if (elemts.length-1 < endIndex) {int newCapcti= default_capacity * 2 Elemts= Arrays.copyOf (elemts,newCapcti);} elemts [endIndex++] = t; return (T []) elemts;} public int size () {return endIndex;} public T get (int I) {if (I < endIndex) {return (T) elemts [I];} throw new RuntimeException ("Index out of bounds") } public static void main (String [] args) {Gys gys=new Gys (); gys.add (5); gys.add (45); System.out.println (gys.get (0)); System.out.println (gys.get (1));}}

The above code can't realize the function of Iterator, and it can't point out the hint of Iterator under idea.

So I can only look through the jdk original code. See the following code in ArrayList.

Declare an inner class Itr in ArrayList, inherit the interface Iterator, and then implement the hasNext () and next () methods.

Define a method that specifically gets an iterator instance.

Public Iterator iterator () {return new Itr ();}

Only then do I understand how to implement the iterator function.

So modify the generic code above.

Package a public import java.util.Arrays;import java.util.Iterator;public class Gys {private final static int default_capacity = 10; private int endIndex = 0; private Object [] elemts; public Gys () {this.elemts = new object [default _ object];} public T [] add (T t) {if (elemts.length-1 < endIndex) {int newCapcti= default_capacity * 2; elemts= Arrays.copyOf (elemts,newCapcti) } elemts [endIndex++] = t; return (T []) elemts;} public int size () {return endIndex;} class Itr implements Iterator {private int point; private int len; public Itr () {this.point=0; this.len=endIndex;} @ Override public boolean hasNext () {return point

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report