//在此编写你的代码
ArrayList<String> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 10; i++) {
list.add(new String());
}
for (int i = 10; i > 0; i--) {
String str = reader.readLine();
list.set(i - 1, str);
}
for (String str : list) {
System.out.println(str);
}
除了三个循环还有更好的方法吗,我只能想到这种help
正在讨论
评论 (3)
- 受欢迎
- 新
- 旧
你必须先登录才能发表评论
Edson
26 十一月 2020, 09:33
题意是一个堆的意思
public class Solution {
public static void main(String[] args) throws Exception {
//在此编写你的代码
ArrayList<String> list = new ArrayList<String>();
java.util.Scanner s = new java.util.Scanner(System.in);
for (int i = 1; i <= 10; i++) {
list.add(null);
}
for (int i = 0; i < 10; i++) {
if (i == 0) {
list.set(i, s.next());
} else {
for (int j = i; j > 0; j--) {
list.set(j, list.get(j - 1));
}
list.set(0, s.next());
}
}
for (String element : list) {
System.out.println(element);
}
}
}
0
yang
13 七月 2020, 14:11
package zh.codegym.task.task07.task0710;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
到达列表顶部
*/
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList <String> list = new ArrayList <String> (10);
BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
for (int i = 0; i < 10; i++)
{
String s = reader.readLine ();
list.add(0, s);
}
for (int i = 0; i< list.size(); i++)
{
System.out.println (list.get(i));
}//在此编写你的代码
}
}
0
Ocean
27 七月 2022, 08:40
I can't understand the usage of 'list.add(0,s)', doesn't every input value replace the original value?
0