小弟现在英国求学..学习JAVA中..近日老师给一题目..如下.1. Create an Array with a capacity of 10 Strings.
2. Fill the Array with 10 different Strings (either interactively by typing
at the keyboard or statically by writing code.
3. Write a line which uses the length property to print the length of the
Array to the terminal.
4. Write code which iterates through the Array, using an Iterator, printing
the elements of the Array to the terminal题目本身不难..就是其中第四条要求对一个Array使用一个Iterator.这里把我难住了.
我也查过API.但是没有收获.这个作业星期五就要交了..小弟心急如焚..望高人指点迷津..
我的代码如下:import java.util.*;class MySecondArray{
public static void main(String [] args){
String[] MyArray = new String[10];
Scanner scanner = new Scanner(System.in);
for(int i = 0;i < 10;i++){
String temp;
System.out.println("Please type in a integer :");
temp = scanner.nextLine();
MyArray[i] = temp;
}
int size = MyArray.length;
System.out.println(size);
Iterator[String] MyIterator = MyArray.iterator();
while(MyIterator.hasNext()){
String temp = MyIterator.next();
System.out.println(temp);
}
}
}
2. Fill the Array with 10 different Strings (either interactively by typing
at the keyboard or statically by writing code.
3. Write a line which uses the length property to print the length of the
Array to the terminal.
4. Write code which iterates through the Array, using an Iterator, printing
the elements of the Array to the terminal题目本身不难..就是其中第四条要求对一个Array使用一个Iterator.这里把我难住了.
我也查过API.但是没有收获.这个作业星期五就要交了..小弟心急如焚..望高人指点迷津..
我的代码如下:import java.util.*;class MySecondArray{
public static void main(String [] args){
String[] MyArray = new String[10];
Scanner scanner = new Scanner(System.in);
for(int i = 0;i < 10;i++){
String temp;
System.out.println("Please type in a integer :");
temp = scanner.nextLine();
MyArray[i] = temp;
}
int size = MyArray.length;
System.out.println(size);
Iterator[String] MyIterator = MyArray.iterator();
while(MyIterator.hasNext()){
String temp = MyIterator.next();
System.out.println(temp);
}
}
}
参考一下吧import java.util.*;public class Test {
@SuppressWarnings("unchecked")
public static void main(String args []){
ArrayList MyArray = new ArrayList();
MyArray.add("123");
MyArray.add("456");
for(Iterator it = MyArray.iterator(); it.hasNext(); ) {
String s = (String)it.next();
System.out.println(s);
}
}
}
这个是对ArrayList用的Iterator...我要的是对Array用的啊....
能解释下这个Iterator类么
Iterator[String] MyIterator = MyArray.iterator();
我这句是有问题的..就是不知道怎么声明一个对ARRAY的ITERATOR...
the elements of the Array to the terminal
是不是要楼主使用Iterator打印一个Array中的元素啊..
觉得应该考察LZ的Arrays这个Helper类的使用. String[] sArray = new String[]{"a","b","c"};
List arrayAsList = Arrays.asList(sArray);
Iterator iterator = arrayAsList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
恩 就是让我用ITERATIR打印里面的元素..
List arrayAsList = Arrays.asList(sArray);
能解释一下这句的意思吗?
返回一个受指定数组支持的固定大小的列表。
也就是将一个Object类型的数组包装成一个集合.
while(MyIterator.hasNext()){
String temp = MyIterator.next();
System.out.println(temp); 这里面不用iterator()方法,改用listIterator()方法:
返回此列表中的元素的迭代器(按适当顺序)。此实现返回 listIterator(0)。 指定者:
接口 List<E> 中的 listIterator
返回:
此列表中的元素的迭代器(按适当顺序)。
另请参见:BTW:建议参考Core java这本书,第二卷<集合>这一章,有讲到ArrayList的使用方法
//sArray:a instance of Array
List arrayAsList = Arrays.asList(sArray);
Iterator it = arrayAsList.Itorator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
应该是不可以用Iterator