実験課題7
Exercise 7(課題7) See the section 4.6.1. "Class of List Structure". Show the execution result of below figure. Further modify the source code of below figure to use array structure, and show its execution result as a screenshot and source code. Follow instructions below to run the program.
- create the Java application project named
chapter4kadai7. - create
mainmethod to run the program. - take the screenshot of the execution result.
- modify the program to use array structure with same execution result.
- take the screenshot of the execution result.
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
String[] terms = input.split(" ");
for (int i = 0; i < terms.length; i = i + 1) {
nums.add(Integer.parseInt(terms[i]));
}
int sum = 0;
for (int n: nums) {
sum = sum + n;
}
System.out.println(sum);
}
(4.6.1.節をよく読み、プロジェクトchapter4kadai7を作成し、図に示すArrayListクラスを使った総和を求めるプログラムを作成し実行結果を報告しなさい。また、ArrayListクラスではなく配列を使うように実行結果が変わらないようにプログラムに変更してソースコードと実行結果を報告しなさい。)