JAVA39. 集合遍历
描述
请使用三种方式遍历一个list集合输入描述
键盘任意输入的五个int类型变量输出描述
使用三种不同的方法遍历集合,遍历输出时不换行,数字之间用空格隔开示例1
输入:
1 2 3 4 5
输出:
普通for循环:1 2 3 4 5 增强for循环:1 2 3 4 5 迭代器遍历:1 2 3 4 5
Go 解法, 执行用时: 3ms, 内存消耗: 1028KB, 提交时间: 2021-11-06
package main import "fmt" /* 一球从h米高度自由落下,每次落地后反弹回原高度的一半再落下,求它在第n次落地时共经过了多少米?第n次反弹多高? 输入描述: 输入小球的初始高度和落地的次数(先输入小球初始高度再输入反弹次数) */ func main() { var a = make([]int, 5, 5) for i := 0; i < 5; i++ { fmt.Scan(&a[i]) } fmt.Printf("普通for循环:") for i := 0; i < 5; i++ { fmt.Printf("%d ", a[i]) } fmt.Printf("\n") fmt.Printf("增强for循环:") j := 0 for j < 5 { fmt.Printf("%d ", a[j]) j++ } fmt.Printf("\n") fmt.Printf("迭代器遍历:") for _, v := range a { fmt.Printf("%d ", v) } }
Java 解法, 执行用时: 27ms, 内存消耗: 10580KB, 提交时间: 2022-02-08
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Integer> list = new ArrayList<>(); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); int num3 = scanner.nextInt(); int num4 = scanner.nextInt(); int num5 = scanner.nextInt(); scanner.close(); list.add(num1); list.add(num2); list.add(num3); list.add(num4); list.add(num5); System.out.print("普通for循环:"); //write your code here...... for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println(); System.out.print("增强for循环:"); //write your code here...... for (int i: list) { System.out.print(i + " "); } System.out.println(); System.out.print("迭代器遍历:"); //write your code here...... Iterator it = list.iterator(); while (it.hasNext()) { System.out.print(it.next() + " "); } System.out.println(); } }
Java 解法, 执行用时: 27ms, 内存消耗: 10600KB, 提交时间: 2022-02-08
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Integer> list = new ArrayList<>(); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); int num3 = scanner.nextInt(); int num4 = scanner.nextInt(); int num5 = scanner.nextInt(); scanner.close(); list.add(num1); list.add(num2); list.add(num3); list.add(num4); list.add(num5); System.out.print("普通for循环:"); //write your code here...... for(int i =0;i<list.size(); i++){ System.out.print(list.get(i)+" "); } System.out.println(); System.out.print("增强for循环:"); //write your code here...... for(int i:list){ System.out.print(i+" "); } System.out.println(); System.out.print("迭代器遍历:"); //write your code here...... Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()){ System.out.print(iterator.next()+" "); } System.out.println(); } }
Java 解法, 执行用时: 29ms, 内存消耗: 10512KB, 提交时间: 2022-02-09
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Integer> list = new ArrayList<>(); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); int num3 = scanner.nextInt(); int num4 = scanner.nextInt(); int num5 = scanner.nextInt(); scanner.close(); list.add(num1); list.add(num2); list.add(num3); list.add(num4); list.add(num5); System.out.print("普通for循环:"); //write your code here...... for(int i = 0; i < list.size(); i++){ System.out.print(list.get(i) + " "); } System.out.println(); System.out.print("增强for循环:"); //write your code here...... for(int li : list){ System.out.print(li + " "); } System.out.println(); System.out.print("迭代器遍历:"); //write your code here...... Iterator it = list.iterator(); while(it.hasNext()){ System.out.print(it.next() + " "); } System.out.println(); } }
Java 解法, 执行用时: 29ms, 内存消耗: 10608KB, 提交时间: 2021-11-20
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Integer> list = new ArrayList<>(); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); int num3 = scanner.nextInt(); int num4 = scanner.nextInt(); int num5 = scanner.nextInt(); scanner.close(); list.add(num1); list.add(num2); list.add(num3); list.add(num4); list.add(num5); System.out.print("普通for循环:"); //write your code here...... for(int i = 0;i<list.size();i++) System.out.print(list.get(i)+" "); System.out.println(); System.out.print("增强for循环:"); //write your code here...... for(int n:list) System.out.print(n+" "); System.out.println(); System.out.print("迭代器遍历:"); //write your code here...... Iterator i = list.iterator(); int tmp; while(i.hasNext()) { System.out.print(i.next()+" "); } System.out.println(); } }