いま、Accountクラスを コレクションフレームワークListに格納したまではうまくいったのですが、
格納した内容を順次表示されるところで、
■C:\Users\owner\Documents\Visual Studio 2010\jcpad231\ソースコード> javac Main4_6.java
Main4_6.java:18: エラー: 不適合な型: Iterator<Account>をList<Account>に変換できません:
List<Account> it = list.iterator();
^
Main4_6.java:19: エラー: シンボルを見つけられません
while( it.hasNext() ) {
^
シンボル: メソッド hasNext()
場所: タイプList<Account>の変数 it
Main4_6.java:20: エラー: シンボルを見つけられません
int i = it.next().getNumber();
^
シンボル: メソッド next()
場所: タイプList<Account>の変数 it
エラー3個
とエラーメッセージがでてしまい、コンパイルが通りません。
C/C++ののりでやっているのですが、やっぱり違うようですね。
ソースコードを載せておきます。
どこが悪いのか、オーバーライドしないといけない関数などがありましたら、
教えていただけないでしょうか?
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
望む結果、 クラスAccount のインスタンスを2つ作り、
それぞれにナンバーを代入、 java.util.* コレクションフレームワークを使って
iterator を使い、中身を表示したい。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
ソースコードを載せておきます。
Account.java
public class Account implements Comparable<Account> {
String accountNo;
int number;
public boolean equals( Object o ) {
if( o == this ) return true;
if( o == null ) return false;
if( !(o instanceof Account)) return false;
Account r = (Account)o;
if( !this.accountNo.trim().equals(r.accountNo.trim())) {
return false;
}
return true;
}
public int compareTo( Account obj ) {
if( this.number < obj.number ) {
return -1;
}
if( this.number > obj.number ) {
return 1;
}
return 0;
}
public int getNumber() {
return this.number;
}
public Account iterator() {
return this;
}
}
import java.util.List.*;
import java.util.*;
public class Main4_6 {
public static void Main( String[] args ) {
List<Account> list = new ArrayList<Account>();
Account a1 = new Account();
a1.number = 10;
Account a2 = new Account();
a2.number = 20;
list.add( a1 );
list.add( a2 );
Collections.sort(list);
// list のソートされた内容をiterator を使って表示したい。
List<Account> it = list.iterator();
while( it.hasNext() ) {
int i = it.next().getNumber();
System.out.println( it );
}
}
}