← Back to feed
Renewal·서른의 생활코딩

Day 7) Array 20180607_am

NS
normalstory
cover image

package d_array;

public class ArrayBasic {

public static void main(String[] args) {

// Array = one variable storing multiple values

/*

1. What is an array?

- Handling multiple variables of the "same type" as a single bundle 

ex) int mathScore =40;

int engScore  =90;

int sciScore  =60;

int korScore =100;

int freScore =20;

// Same type! 

2. Array declaration 

- Declare a variable of the desired type and use [brackets] to indicate it's an array

int[] score; // reference type, stores an address 

int score[]; // you can't tell from the type alone whether it's an array

3. Not called initialization, but creation. Because it holds a value to reference. To make an address. 

- To create an array, use the 'new' operator // <-- same as creating a new address,

along with specifying the array's type and size. 

*/

// Declaration method 1

int[] score = new int[4]; // required, number, size // initialized with default value

// System.out.println(score);// made randomly 

// System.out.println(score[0]);// initialized with default value of declared type

// System.out.println(score[1]);// initialized with default value of declared type

// System.out.println(score[2]);// initialized with default value of declared type 

// System.out.println(score[3]);// initialized with default value of declared type

// System.out.println(score[4]);// starts from 0~~!!!

// Example 1) implement with for loop

// for(int i=0; i<5 ; i++){

// System.out.println("array slot "+i+":"+score[i]);

// }

for(int i=0; i<score.length ; i++){

System.out.println("array slot "+i+":"+score[i]);

}

// Example 2) assigning values to array 

//0-> 0 score[0]=i*10;

//1->10 score[1]=i*10;

//2->20 score[2]=i*10;

//3->30 put score[3]=i*10; 

for(int j=0; j<score.length; j++){

score[j]=j*10;

//System.out.println("array slot "+j+" value :"+score[j]);

}

//

// // Declaration method 2

// int[] score2 = new int[]{1,2,3,4}; // uninitialized slots are 0, // must have a number after ,

// System.out.println("declaration method 2"+score2[i]);

//

// // Declaration method 3

// int[] score3 = {1,2,3,4}; // when declaring with initialization, can be omitted

// System.out.println("declaration method 2"+score3[i]);

//1. Declare and create variable score2 that can store 6 subjects' scores

int[] score2 = new int[5];

//2. Initialize each slot of score2 with a random value between 0 and 100

for(int i=0; i<7;i++){

score2[i]= (int)Math.random()*100+0;

}

//3. Print all subject scores arg

System.out.println(score2[0]);

System.out.println(score2[1]);

System.out.println(score2[2]);

System.out.println(score2[3]);

System.out.println(score2[4]);

System.out.println(score2[5]);

//4. Calculate sum of subjects sum 

//5. Express the subject average rounded to 2 decimal places

//6. Find the maximum among all subjects — max (do not hard-cap at 100)

//7. Find the minimum among all subjects — min (do not hard-cap at 0)

}

}



This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.