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

Day 8) Arrays 20180608_am

NS
normalstory
cover image


package d_array;

public class ArrayBasic {

public static void main(String[] args) {

// Array = one variable stores multiple values

/*

1. What an array is

- Bundling multiple variables of "the same type" into one group 

ex) int mathScore =40;

int engScore  =90;

int sciScore  =60;

int korScore =100;

int freScore =20;

// same type! 

2. Declaring an array 

- Declare a variable of the desired type and use [square brackets] on the variable or type to show it's an array

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

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

3. It's called 'creation', not 'initialization'. Because it holds a reference value. To make an address. 

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

and you must specify the array type and size together 

*/

//

// // Declaration style 1

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

//// System.out.println(score);// gets created randomly 

//

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

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

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

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

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

//

// // Example 1) implement with a 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) put values into the array 

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

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

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

// // put 3->30 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 style 2

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

// System.out.println("Declaration style 2"+score2[i]);

//

// // Declaration style 3

// int[] score3 = {1,2,3,4}; // when declaring and initializing together, some parts can be omitted

// System.out.println("Declaration style 2"+score3[i]);

//

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

// int[] score2 = new int[6];

// 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()*101+0);

// }

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

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

// }

//

//// // 3. Print scores for all subjects 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[8]);

//

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

// System.out.println(score2[i]);

// }

// // 4. Compute the sum of the subject scores 

// int sum =0;

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

// sum = sum + j;

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

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

// System.out.println(score2[j]);

// }

// System.out.println("SUM : "+sum);

// }

// int[] score2 = new int[6];

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

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

// }

// int sum =0;

//// sum += score2[0];

//// sum += score2[1];

//// sum += score2[2];

//// sum += score2[3];

//// sum += score2[4];

//// sum += score2[5];

//

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

// sum = sum + score2[i];

// }

// System.out.println(sum);

//

// int[] score2 = new int[6];

// int sum =0;

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

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

// score2[j]= (int)(Math.random()*101+0);

// }

// sum = sum + score2[i];

// }

// System.out.println("SUM : "+sum);

//

//

// // 5. Show the average of the subject scores rounded at the 3rd decimal place to 2 decimal places

//

// int[] score2 = new int[6];

//

// for(int i=0; i<score2.length;i++){// random value

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

// }

//

// int sum =0;// sum

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

// sum = sum + score2[i];

// }

// System.out.println(sum);

//

// float avg = (sum/(float)(score2.length));// average

// System.out.println(avg);

// avg =(int)(avg*100+0.5)/100f;// fix decimal places 

// System.out.println(avg);

//

// // 6. Find the maximum among all subjects - max (but don't hardcode 100 as the max)

// //if((score2[i] <=score2[i+1])? score2[i]=max ; score2[i]=max )

// // can't I do it like breakout (brick-breaking)?

int[] score2 = new int[6];

for(int i=0; i<score2.length;i++){// random value

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

}

int sum =0;// sum

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

sum = sum + score2[i];

}

System.out.println(sum);


float avg = (sum/(float)(score2.length));// plain average

System.out.println(avg);

avg =(int)(avg*100+0.5)/100f;// round the plain average and fix decimals 

System.out.println(avg);

// int sum = 0;

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

// if(){

// }

// }


// int sum = 0;

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

// //max = (score2[i] > score2[i+1])? max ; 

// }

// int num1 = score2[0];

// for(int i =1 ; i<7 ; i++){

// int num2 = score2[i];

// if(num1>num2){

// num1 = score2[i];

// }

// }

// System.out.println(" max :" + num1 );

// // 7. Find the minimum among all subjects - min (but don't hardcode 0 as the min)

//

}

}


Eclipse export 20170608.zip
Download

This English version was translated by Claude.

zipEclipse export 20170608.zip
친절한 찰쓰씨
Written by
친절한 찰쓰씨

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

More on the author's page

Keep reading

Renewal

Steadily, for the long haul, without burning out

Mar 31, 2026·9 min
Renewal

Tech-life balance

Feb 7, 2026·3 min
Renewal

Humanality, by Park Jeong-ryeol

Feb 7, 2026·11 min