Java開発でEnumerationインタフェースの使い方
Javaコード:
package com.startnews24.study;
import java.util.Enumeration;
@SuppressWarnings(“rawtypes")
class TestEnumeration implements Enumeration
{
int count;
int length;
Object[] dataArray;
TestEnumeration(int count, int length, Object[] dataArray) {
this.count = count;
this.length = length;
this.dataArray = dataArray;
}
public boolean hasMoreElements() {
return (count < length);
}
public Object nextElement() {
return dataArray[count++];
}
}
class TestDataStruct
{
String[] obj;
TestDataStruct() {
obj = new String[4];
obj[0] = “品川-11";
obj[1] = “品川-12";
obj[2] = “品川-13";
obj[3] = “品川-14";
}
@SuppressWarnings(“rawtypes")
Enumeration getEnum() {
return new TestEnumeration(0,obj.length,obj);
}
}
public class EnumerationDemo {
public static void main(String[] args) {
TestDataStruct cntruct = new TestDataStruct();
@SuppressWarnings(“rawtypes")
Enumeration cnEnumeration =cntruct.getEnum();
while (cnEnumeration.hasMoreElements())
System.out.println(cnEnumeration.nextElement());
}
}
実行結果:
品川-11
品川-12
品川-13
品川-14