在Java中,我被要求在单链表中存储整数值,然后打印存储在列表中的元素.这是我想出的:
int max = 10;
List<Integer> list = new ArrayList<Integer>();
for (int num = 0; i < max; i++){
list.add(num);
}
System.out.print(list);
Run Code Online (Sandbox Code Playgroud)
我想知道,ArrayList和单链表一样吗?我想确保我正确地回答了这个问题.这有意义吗?谢谢!
我目前正在制作一个程序,要求输入两个团队的名称和分数.当我请求输入名称和第一组的9分时,扫描仪接受输入就好了.但是,在for循环之后,扫描程序不接受第二个团队名称的输入.这不是整个程序,但我已经包含了所有代码,直到它给我带来麻烦.我怀疑它可能与for循环有关,因为当我将它放在for循环之前,team2接受用户输入就好了.
import java.util.Scanner;
public class sportsGame{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String team1;
String team2;
int team1Scores[] = new int[9]
int team1Total = 0;
int team2Scores[] = new int[9];
int team2Total = 0;
System.out.print("Pick a name for the first team: ");
team1 = input.nextLine();
System.out.print("Enter a score for each of the 9 innings for the "
+ team1 + " separated by spaces: ");
for(int i = 0; i < team1Scores.length; i++){
team1Scores[i] = input.nextInt(); …Run Code Online (Sandbox Code Playgroud)