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

ArrayBasic

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 tell right away if 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 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 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)?

// no - to compare one at a time, you have to do it like an ideal-type world-cup bracket

// pick one as aaa[0] and start the battle. Keep comparing that pick against the rest as it moves up.

int[] score2 = new int[6];

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

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

System.out.print(score2[i]+" ");

}

int sum =0;// sum

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

sum = sum + score2[i];

}

System.out.println("sum "+sum);

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

System.out.println("avg1 "+avg);

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

System.out.println("avg2 "+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 );

// int max = score2[0];

// if(max<score2[1]){

// max=score2[1];

// }

// if(max<score2[2]){

// max=score2[2];

// }

// if(max<score2[3]){

// max=score2[3];

// }...

int max = score2[0];

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

if(score2[0]<score2[i]){

score2[0]=score2[i];

}

}

System.out.println("max "+score2[0]);


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

int min = score2[0];

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

if(score2[0]>score2[i]){

score2[0]=score2[i];

}

}

System.out.println("min "+score2[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.