Java入門–Listの要素(for-each、iterator、object)を操作する

1.javaコード
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Listdatademo {

@SuppressWarnings(“unchecked")
public static void main(String[] args) {
@SuppressWarnings(“rawtypes")
List list = new ArrayList();
list.add(“tokyo");
list.add(“oosaka “);
list.add(“kanagawa");
for(int i = 0 ; i< list.size();i++){

System.out.println(“list function:"+list.get(i));
}
//for each
for(Object o:list){
System.out.println(“for each:"+o);
}
@SuppressWarnings(“rawtypes")
Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(“iterator:"+it.next());
}
Object o[];
o = list.toArray();
for(int i = 0 ; i <o.length;i++){
System.out.println(“object:"+o[i]);
}
//toString
System.out.println(list.toString());

}

}

2.実行結果
list function:tokyo
list function:oosaka
list function:kanagawa
for each:tokyo
for each:oosaka
for each:kanagawa
iterator:tokyo
iterator:oosaka
iterator:kanagawa
object:tokyo
object:oosaka
object:kanagawa
[tokyo, oosaka , kanagawa]

Java

Posted by arkgame