Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kodaitakahashi
Created December 18, 2016 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kodaitakahashi/99bdbe59106c92e816da19ebfee19298 to your computer and use it in GitHub Desktop.
Save kodaitakahashi/99bdbe59106c92e816da19ebfee19298 to your computer and use it in GitHub Desktop.
Java Iteratour
package iterator;
import java.util.ArrayList;
/**
* Userを格納するためのクラス
* @author kodaitakahashi
*
*/
public class UserList {
private ArrayList<User> userList = new ArrayList<User>();
/**
* userListにuserを格納する
* @param user 格納するUserのインスタンス
*/
public void appendUser(User user) {
// TODO Auto-generated method stub
userList.add(user);
}
/**
* 引数で受け取ったindexの要素を取得する
* @param index 配列のindex
* @return User userListから取得した要素
*/
public User getUserAt(int index){
return (User)userList.get(index);
}
/**
* userListの要素数を取得
* @return int 要素数
*/
public int getLength() {
return userList.size();
}
/**
* Iteratorのインタフェースに基づいたUserListIteratorを返す
* @return UserListIntarator
*/
public Iterator iteretor(){
return new UserListIterator(this);
}
}
package iterator;
public class Main1 {
public static void main(String[] args){
UserList userList = new UserList();
userList.appendUser(new User("ookubo", 0));
userList.appendUser(new User("syohey", 1));
Iterator it = userList.iteretor();
while(it.hasNext()){
User user = (User)it.next();
System.out.println(user);
}
while(it.hasPrev()) {
User user = (User)it.prev();
System.out.println(user);
}
}
}
package iterator;
/**
* ユーザの情報を管理するクラス
* @author Kodai Takahashi
*
*/
public class User {
private String name;
private int sex;
/**
*
* @param name ユーザの名前
* @param sex ユーザの性別
* @return なし
*
*/
public User(String name, int sex) {
this.name = name;
this.sex = sex;
}
/**
* nameを返す
* @return String name
*/
public String name() {
return this.name;
}
/**
* sexを返す
* @return int sex
*/
public int sex() {
return this.sex;
}
/**
* 表示フォーマットを定義
*/
public String toString(){
return "name: " + this.name + " sex: " + this.sex;
}
}
package iterator;
import java.util.ArrayList;
/**
* Userを格納するためのクラス
* @author kodaitakahashi
*
*/
public class UserList {
private ArrayList<User> userList = new ArrayList<User>();
/**
* userListにuserを格納する
* @param user 格納するUserのインスタンス
*/
public void appendUser(User user) {
// TODO Auto-generated method stub
userList.add(user);
}
/**
* 引数で受け取ったindexの要素を取得する
* @param index 配列のindex
* @return User userListから取得した要素
*/
public User getUserAt(int index){
return (User)userList.get(index);
}
/**
* userListの要素数を取得
* @return int 要素数
*/
public int getLength() {
return userList.size();
}
/**
* Iteratorのインタフェースに基づいたUserListIteratorを返す
* @return UserListIntarator
*/
public Iterator iteretor(){
return new UserListIterator(this);
}
}
package iterator;
/**
* イテレーターの機能を実装したクラス
* interface Iterator
* @author kodaitakahashi
*
*/
public class UserListIterator implements Iterator{
private UserList userList;
private int index;
/**
* userListにUserListのインスタンスを代入 要素数indexを初期化する
* @param usertList UserListのインスタンス
*/
public UserListIterator(UserList usertList) {
this.userList = usertList;
this.index = 0;
}
public boolean hasNext(){
if(index < userList.getLength()){
return true;
}else{
return false;
}
}
public boolean hasPrev() {
if(index > 0){
return true;
}else{
return false;
}
}
public Object next() {
User user = userList.getUserAt(index);
index++;
return user;
}
public Object prev() {
index--;
User user = userList.getUserAt(index);
return user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment