Untitled
unknown
java
a year ago
2.1 kB
6
Indexable
/* nested for-loop: outer loop(外面的循环): the number of rows (循环行数) inner loop (里面的循环): the number of columns (循环列数) Array create an integer array: int[] array_name = new int[length]; int[] arr2 = {4, 6, -3, 23, 1, 5, -1, -4, 100}; **The length of array is FIXED **Index is used to access/modify array element (用索引(index) 来提取、改变某一个信息) index:0 ~ (length-1). 索引从 0 开始, 到 (array 长度 - 1) 停止 */ import java.util.*; import java.lang.Math; public class Main { public static void main(String[] args) { /* * *** ***** ******* ********* *********** */ //只需要打符号前面的空格,不需要打后面的 //Solution: for(int i=0; i < 6; i++) { for(int j = 5; j > i; j--) { System.out.print(' '); } for(int j = 0; j < (i*2+1); j++) { System.out.print('*'); } System.out.println(); } //create an int array of size 10 with numbers of 1 to 10 in increasing order int N = 20; int[] arr = new int[N]; for(int i = 0; i < N; i++) { arr[i] = i+1; } // find the minimum value in the Arrays // find the current minimum and compare with the current number N = 9; int[] arr2 = {4, 6, -3, 23, 1, 5, -1, -4, 100}; int current_smallest = arr2[0]; for(int i = 0; i < N; i++) { if(current_smallest > arr2[i]) { current_smallest = arr2[i]; } System.out.println(current_smallest); } //create an array, where the ith number stores the value of 2^i, 0 <= i <= N-1 N = 15; int[] arr3 = new int[N]; for(int i = 0; i < N; i++) { int product = 1; for(int j = 0; j < i; j++) { product *= 2; } arr3[i] = product; } System.out.println(Arrays.toString(arr3)); } }