๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

Java (5) - ๋ฐฐ์—ด ๋ณธ๋ฌธ

Language/Java

Java (5) - ๋ฐฐ์—ด

๊ธฐ๋Œ 2023. 10. 27. 17:56

๐Ÿ“š ๊ตญ๋น„ 15 ์ผ์ฐจ


 

 

 

๐Ÿ”น ๋ฐฐ์—ด

  • ๊ฐ™์€ ํƒ€์ž…์˜ ๊ฐ’์„ ์—ฌ๋Ÿฌ๊ฐœ ์—ฐ์†์ ์œผ๋กœ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด๋‹ค
  • ๋ฐฐ์—ด ๊ฐ์ฒด์˜ ๊ฐ ์นธ์—๋Š” 0 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” index ๊ฐ€ ์ž๋™์œผ๋กœ ๋ถ€์—ฌ ๋œ๋‹ค.
  • Java์˜ ๋ฐฐ์—ด์€ ํ•œ๋ฒˆ ์ •ํ•ด์ง„ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
    (๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ๋•Œ ํฌ๊ธฐ๋ฅผ ๋ฐ˜๋“œ์‹œ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ , ๋ฐฐ์—ด์— ์ €์žฅํ•  ๊ฐ’์„ ๊ตฌ์ฒด์ ์œผ๋กœ ๋‚˜์—ดํ•ด์•ผ ํ•œ๋‹ค)
  • ๋ฐฐ์—ด๊ฐ์ฒด์˜ ๊ฐ ์นธ์€ ํ•ด๋‹น ํƒ€์ž…์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ž๋™์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ๋‹ค.

 

// ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•ด ๋ฐฐ์—ด ๊ฐ์ฒด ์ƒ์„ฑ

int [] arr = { 10 , 20 , 30 };
double [] arr2 = { 0.1 , 7.2 , 6.9 , 7.7 };
String [] arr3 = {"์ถ•์œต", "ํ•˜๋ˆ„๋งŒ" , "๊ฐ€๋„ค์ƒค" };

๐Ÿ”น ๋ฐฐ์—ด ์•ก์„ธ์Šค ( ๋ฐฐ์—ด์— ์ ‘๊ทผํ•˜๊ธฐ )

  • ๋ฐฐ์—ด์˜ ๊ฐ ์ €์žฅ์†Œ์— ์•ก์„ธ์Šค๋Š” ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
// ๋ฐฐ์—ด์— ๊ฐ’ ์ €์žฅํ•˜๊ธฐ

์ฐธ์กฐ๋ณ€์ˆ˜๋ช…[์ธ๋ฑ์Šค] = ๊ฐ’ ;

// ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๊ฐ’ ์‚ฌ์šฉํ•˜๊ธฐ

System.out.println(์ฐธ์กฐ๋ณ€์ˆ˜๋ช…[์ธ๋ฑ์Šค]);

 

๐Ÿ”น ๋ฐฐ์—ด์˜ ๊ธธ์ด ์กฐํšŒํ•˜๊ธฐ

  • ์ฐธ์กฐ๋ณ€์ˆ˜๋ช….length;
  • ๋ฐฐ์—ด๊ฐ์ฒด์—๋Š” length ๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋‹ค
  • ํ”„๋กœํผํ‹ฐ - ๊ฐ์ฒด์˜ ๊ตฌ์„ฑ์š”์†Œ , ๊ฐ์ฒด์˜ ๊ณ ์œ ํ•œ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค.
	System.out.println("arr1 ๋ฐฐ์—ด์˜ ๊ธธ์ด : "+ arr1.length);

 


๐Ÿ”น ๋ฐฐ์—ด๊ฐ์ฒด ํ™œ์šฉ

  • ๋ฐฐ์—ด๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ชฉ์ ์€ ๊ฐ™์€ ์ข…๋ฅ˜์™€ ๊ฐ™์€ ํƒ€์ž…์˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ์ด๋ฆ„์œผ๋กœ ๊ด€๋ฆฌํ• ์ˆ˜ ์žˆ๋‹ค
  • ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•ด ๋ฐฐ์—ด์˜ ๊ฐ ์ €์žฅ์†Œ๋ฅผ ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
		// ์˜ˆ์‹œ
		String name1 = "๊น€์œ ์‹ ";
		String name2 = "๊ณฝ์žฌ์šฐ";
		String name3 = "๊น€์‹œ๋ฏผ";
		String name4 = "๊ถŒ์œจ";
        
                System.out.println(name1);
                System.out.println(name2);
                System.out.println(name3);
                System.out.println(name4);
        
		String[] names = { "๊น€์œ ์‹ ", "๊ณฝ์žฌ์šฐ" ,"๊น€์‹œ๋ฏผ" ,"๊ถŒ์œจ" };
		for(int index = 0; index < 4; index++) {
                    	System.out.println(names[index]);
				}

 

๐Ÿ”น ๋ฐฐ์—ด๊ณผ ๊ด€๋ จ๋œ ์˜ค๋ฅ˜๋‚ด์šฉ ํ™•์ธํ•˜๊ธฐ

int [] arr1 = {10 , 20 , 30};

System.out.println(arr1[0]);
System.out.println(arr1[1]);
System.out.println(arr1[2]);
System.out.println(arr1[3]);

// ์ธ๋ฑ์Šค ๋ฒ—์–ด๋‚จ

/*Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 
* Index 3 out of bounds for length 3 at array.Array04.main(Array04.java:18)
*/

 


 

 


์‹ค์Šต

 

โ—ป ๋ฐฐ์—ด ์ƒ์„ฑ

// ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•ด ๋ฐฐ์—ด ๊ฐ์ฒด ์ƒ์„ฑ

int [] arr = { 10 , 20 , 30 };
double [] arr2 = { 0.1 , 7.2 , 6.9 , 7.7 };
String [] arr3 = {"๊ณฝ์žฌ์šฐ", "์นธ" , "๋‚˜ํด๋ ˆ์˜น" };
// new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋นˆ ๋ฐฐ์—ด๊ฐ์ฒด ์ƒ์„ฑ
// ๋ฐฐ์—ด ํฌ๊ธฐ ์ƒ๋žต ๋ถˆ๊ฐ€

int [] arr1 = new int[5];  // ์ •์ˆ˜ 5๊ฐœ ๋‹ด์„์ˆ˜ ์žˆ๋Š” ๋นˆ ๋ฐฐ์—ด๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ์ฃผ์†Œ๊ฐ’์„ arr1์— ๋Œ€์ž…
double [] arr2 = new double[4]; // ์‹ค์ˆ˜ 4๊ฐœ ๋‹ด์„์ˆ˜ ์žˆ๋Š” ๋นˆ ๋ฐฐ์—ด๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ์ฃผ์†Œ๊ฐ’์„ arr2์— ๋Œ€์ž…
String [] arr3 = new String[2]; // ๋ฌธ์ž์—ด 2๊ฐœ๋ฅผ ๋‹ด์„์ˆ˜ ์žˆ๋Š” ๋นˆ ๋ฐฐ์—ด๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ์ฃผ์†Œ๊ฐ’์„ arr3์— ๋Œ€์ž…
// new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐฐ์—ด๊ฐ์ฒด ์ƒ์„ฑ

int [] arr6 = new int[] {10 , 20 , 30};
double [] arr7 = new double[] {3.14 , 2.76 , 12.25};
String [] arr8 = new String[] {"ํ•˜๋‚จ","๊ตฌ๋ฆฌ","๋‹ค์‚ฐ"};

 


โ—ป ๋นˆ ๋ฐฐ์—ด๊ฐ์ฒด์˜ ๊ฐ’ ํ™•์ธํ•˜๊ธฐ

		// new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋นˆ ๋ฐฐ์—ด๊ฐ์ฒด ์ƒ์„ฑ
		// ๋นˆ ๋ฐฐ์—ด๊ฐ์ฒด์˜ ๊ฐ ์ €์žฅ์†Œ์—๋Š” ์ €์žฅ๋œ ํƒ€์ž…์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
		int [] arr1 = new int[3];
		double [] arr2 = new double[3];
		String [] arr3 = new String[3];
		
		// ๊ฐ์ฒด์˜ ๊ฐ ์ €์žฅ์†Œ์˜ ๊ฐ’์„ ํ™•์ธํ•˜๊ธฐ
		System.out.println(arr1[0]);	// 0
		System.out.println(arr1[1]);	// 0
		System.out.println(arr1[2]);	// 0
		
		
		System.out.println(arr2[0]);	// 0.0
		System.out.println(arr2[1]);	// 0.0
		System.out.println(arr2[2]);	// 0.0
		
		System.out.println(arr3[0]);	// null
		System.out.println(arr3[1]);	// null
		System.out.println(arr3[2]);	// null

 


โ—ป ๋ฐฐ์—ด๊ฐ์ฒด์˜ ๊ฐ’ ์ €์žฅํ•˜๊ธฐ

	// new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋นˆ ๋ฐฐ์—ด๊ฐ์ฒด ์ƒ์„ฑ
		// ๋นˆ ๋ฐฐ์—ด๊ฐ์ฒด์˜ ๊ฐ ์ €์žฅ์†Œ์—๋Š” ์ €์žฅ๋œ ํƒ€์ž…์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
		int [] arr1 = new int[3];
		double [] arr2 = new double[3];
		String [] arr3 = new String[3];
		
		
		// ๋ฐฐ์—ด๊ฐ์ฒด 0 ๋ฒˆ์งธ ์ €์žฅ์†Œ์— ๊ฐ’ ์ €์žฅํ•˜๊ธฐ
		arr1[0] = 10;
		arr2[0]	= 2.8;
		arr3[0] = "๋‚˜ํƒ€";
		
		
		// ๊ฐ์ฒด์˜ ๊ฐ ์ €์žฅ์†Œ์˜ ๊ฐ’์„ ํ™•์ธํ•˜๊ธฐ
		System.out.println(arr1[0]);
		System.out.println(arr1[1]);
		System.out.println(arr1[2]);
		
		
		System.out.println(arr2[0]);
		System.out.println(arr2[1]);
		System.out.println(arr2[2]);
		
		System.out.println(arr3[0]);
		System.out.println(arr3[1]);
		System.out.println(arr3[2]);

 

'


โ—ป ๋ฐฐ์—ด๊ฐ์ฒด ๊ธธ์ด ์กฐํšŒํ•˜๊ธฐ

System.out.println("arr1 ๋ฐฐ์—ด์˜ ๊ธธ์ด : "+ arr1.length);
System.out.println("arr2 ๋ฐฐ์—ด์˜ ๊ธธ์ด : "+ arr2.length);
System.out.println("arr3 ๋ฐฐ์—ด์˜ ๊ธธ์ด : "+ arr3.length);

 


 

โ—ป ๋ฐฐ์—ด ํ™œ์šฉ

/*
     * ๋ฐฐ์—ด ๊ฐ์ฒด์˜ ํ™œ์šฉ
     * 		- ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•ด ๋ฐฐ์—ด์˜ ๊ฐ ์ €์žฅ์†Œ๋ฅผ ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
     * 		- ๋ฐฐ์—ด๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ชฉ์ ์€ ๊ฐ™์€ ์ข…๋ฅ˜์™€ ๊ฐ™์€ ํƒ€์ž…์˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ์ด๋ฆ„์œผ๋กœ ๊ด€๋ฆฌ ํ• ์ˆ˜ ์žˆ๋‹ค.
     * 			
     * 		์˜ˆ์‹œ ) 
     * 			ํšŒ์›์ด๋ฆ„
     * 				String name1 = "๊น€์œ ์‹ ";
     * 				String name2 = "๊ณฝ์žฌ์šฐ";
     * 				String name3 = "๊น€์‹œ๋ฏผ";
     * 				String name4 = "๊ถŒ์œจ";
     *  
     *  			String[] names = { "๊น€์œ ์‹ ", "๊ณฝ์žฌ์šฐ" ,"๊น€์‹œ๋ฏผ" ,"๊ถŒ์œจ" };
     *  			
     */



    /*
     * ๊ตญ์–ด ์˜์–ด ์ˆ˜ํ•™ ์ ์ˆ˜๊ฐ€ ์ €์žฅ๋œ ๋ฐฐ์—ด์ด ์žˆ๋‹ค
     * ๊ตญ์–ด ์ ์ˆ˜์˜ ํ‰๊ท  , ์˜์–ด ์ ์ˆ˜์˜ ํ‰๊ท  , ์ˆ˜ํ•™ ์ ์ˆ˜์˜ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜๋ผ
     */

    int [] korScores = {100 , 80 , 80 , 90 , 60 , 70};
    int [] engScores = {90 , 80 , 60 , 70 , 60 , 60};
    int [] mathScores = {100 , 60 , 50 , 80 , 40 , 60};


    int korTotal = 0;
    int engTotal = 0;
    int mathTotal = 0;

    // ๊ณผ๋ชฉ๋ณ„ ์ด์ 

//		for( int value : korScores) {
//			korTotal += value;
//		}
//		for( int value : engScores) {
//			engTotal += value;
//		}
//		for( int value : mathScores) {
//			mathTotal += value;
//		}



for(int index = 0; index < korScores.length; index++) {
        korTotal += korScores[index];
    }

    for(int index = 0; index < engScores.length; index++) {
        engTotal += engScores[index];
    }

    for(int index = 0; index < mathScores.length; index++) {
        mathTotal += mathScores[index];
	}


    // ํ‰๊ท 
    int korAvg = korTotal / korScores.length;
    int engAvg = engTotal / engScores.length;
    int mathAvg = mathTotal / mathScores.length;

    System.out.println("๊ตญ์–ด ์ ์ˆ˜ ํ‰๊ท  : " + korAvg);
    System.out.println("์˜์–ด ์ ์ˆ˜ ํ‰๊ท  : " + engAvg);
    System.out.println("์ˆ˜ํ•™ ์ ์ˆ˜ ํ‰๊ท  : " + mathAvg);

 

โ—ป ๋ฐฐ์—ด ๋‹ค๋ฃจ๊ธฐ feat. Scanner ๊ฐ์ฒด

package Java.array;

import java.util.Arrays;
import java.util.Scanner;

public class Array07 {
    public static void main(String[] args) {

        /*
         * ์ž„์˜์˜ ์ˆซ์ž 10 ๊ฐœ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ 
         * ๋ณต์‚ฌ์‹œ์ž‘์œ„์น˜ , ๋ณต์‚ฌ ์ข…๋ฃŒ ์œ„์น˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„
         * ์›๋ณธ ๋ฐฐ์—ด์—์„œ ํ•ด๋‹ด๋ฒ”์œ„์˜ ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ธฐ
         */

        // ์ •์ˆ˜ 10๊ฐœ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด ๊ฐ์ฒด ์ƒ์„ฑ , ๊ทธ ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’์„ numbers ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•œ๋‹ค
        int [] numbers = new int[10];

        // numbers ๋ฐฐ์—ด์— ์ž„์˜์˜ ์ˆซ์ž ์ง‘์–ด๋„ฃ๊ธฐ
        // ์ž„์˜์˜ ์ˆซ์ž -> math.random() ์‚ฌ์šฉ

        for(int index = 0; index < numbers.length; index++){
            // 1 ~ 100 ์ž„์˜์˜ ์ •์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์ˆœ์„œ๋Œ€๋กœ ์ง‘์–ด๋„ฃ๊ธฐ
            numbers[index] = (int)(Math.random() * 100 ) +1 ;
        }
        System.out.println(Arrays.toString(numbers));



        // Scanner ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‹œ์ž‘์œ„์น˜ ์ข…๋ฃŒ์œ„์น˜ ์ฐพ๊ธฐ
        Scanner scanner = new Scanner(System.in);

        // ์‹œ์ž‘์œ„์น˜
        System.out.println("์‹œ์ž‘์œ„์น˜ ์ธ๋ฑ์Šค : ");
        int start = scanner.nextInt();

        // ์ข…๋ฃŒ์œ„์น˜
        System.out.println("์ข…๋ฃŒ ์œ„์น˜ : ");
        int end = scanner.nextInt();

        // ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์‹œ์ž‘ ์œ„์น˜ ~ ์ข…๋ฃŒ ์œ„์น˜๊นŒ์ง€ ๋ณต์‚ฌํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ธฐ
        int[] subArr = new int [end - start +1];

        // ์›๋ณธ ๋ฐฐ์—ด์—์„œ ์ƒˆ๋ฐฐ์—ด๋กœ ๊ฐ’ ๋ณต์‚ฌ
        int position = 0;
        for(int index = start; index < end; index++){
            subArr[position] = numbers[index];
            position++;
        }

        // ๋ฐฐ์—ด ์ถœ๋ ฅํ•˜๊ธฐ for - each
        for ( int value : subArr){
            System.out.print(value + " ");
        }
        System.out.println();
    }
}

โ—ป  ๋ฐฐ์—ด์— ํ•ด๋‹น ์ˆซ์ž ์œ„์น˜ (index) ์ฐพ๊ธฐ

package Java.array;

public class Array06 {
    public static void main(String[] args) {
        // ์ง€์ •๋œ ๋ฐฐ์—ด์— ์ž…๋ ฅํ•œ ์ˆซ์ž๊ฐ€ ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ์•Œ์•„๋‚ด๊ธฐ
        // ๋ฐฐ์—ด์—์„œ ๊ทธ ๊ฐ’์ด ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ์กฐ์‚ฌํ•ด์„œ ์ถœ๋ ฅ
        // ๋‹จ , ๊ทธ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด -1 ์„ ์ถœ๋ ฅ


        // ๋ฐฐ์—ด
        int []  numbers = {10 , 20 ,300 ,40 , 50 , 60 ,70 };

        // 40 ์ด ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ์•Œ์•„๋‚ด๊ณ ์‹ถ๋‹ค.
        int position = -1;
        // -1์€ ์œ ํšจํ•œ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹ˆ๋ฉฐ "๊ฐ’์„ ์ฐพ์ง€ ๋ชปํ–ˆ๋‹ค"๊ณ  ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด
        // ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋Š” [0] ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋‹ˆ๊น
        // ๊ฐ’์ด ์—†๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ ์ถœ๋ ฅ๊ธ€์— -1 ์ด ์ถœ๋ ฅ์ด๋จ.

        for(int index = 0; index < numbers.length; index++) {
            if(numbers[index] == 40) {
                // if๋ฌธ ์กฐ๊ฑด์œผ๋กœ 40 ์ด๋ผ๋Š” ์ˆซ์ž๊ฐ€ ๋ฐฐ์—ด๋‚ด์— ์กด์žฌํ•œ๋‹ค๋ฉด ์‹คํ–‰๋œ๋‹ค
                position = index;
                // 40 ์ด๋ผ๋Š” ๊ฐ’์ด ํ•ด๋‹น ์ธ๋ฑ์Šค์— ์กด์žฌํ•œ๋‹ค๋ฉด position ๋ณ€์ˆ˜์— ๋‹ด๋Š”๋‹ค
                break;  // ์ดํ›„์— ๋ฐฐ์—ด์—์„œ ์ฃผ์–ด์ง„ 40 ์ด๋ผ๋Š” ๊ฐ™์€ ๊ฐ’์ด ์žˆ์„์ˆ˜ ์žˆ์œผ๋‹ˆ break๋ฌธ ๊ฑธ์Œ
            }
        }

        System.out.println("์œ„์น˜ : "+position);
    }
}

 

โ—ป Scanner๋ฅผ ํ†ตํ•ด ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์— ์ž…๋ ฅํ•œ ๊ฐ’ ์œ„์น˜ ์ฐพ๊ธฐ

package Java.array;

import java.util.Scanner;

public class Array06_01 {
    public static void main(String[] args) {
        int[] arr = {3,13,19,20,25,29};

        Scanner scanner = new Scanner(System.in);
        System.out.println("์ฐพ๊ณ ์ž ํ•˜๋Š” ์ˆซ์ž : ");

        // ๋‚ด๊ฐ€ ์ฐพ์œผ๋ ค ํ•˜๋Š” ์ˆซ์ž๋ฅผ target์— ๋‹ด์Œ
        int target = scanner.nextInt();

        // ๋‚ด๊ฐ€ ์ž…๋ ฅํ•œ ์ˆซ์ž๊ฐ€ ๋ฐฐ์—ด์— ์—†๋‹ค๋ฉด -1 ์„
        int position = -1;

        for(int index = 0; index < arr.length; index++) {
            // index๋Š” ํ•ด๋‹น์œ„์น˜ - ๋ฐฐ์—ด์œ„์น˜
            // target์€ ๋‚ด๊ฐ€ ์ž…๋ ฅํ•œ ์ˆซ์ž
            if(arr[index] == target) {
                position = index;
                // ๋’ค์— ๊ฐ™์€ ์ˆซ์ž ์žˆ์œผ๋ฉด ๋’ค์— ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค๊ณ  ํ•จ ๊ทธ๋ž˜์„œ break;๊ฑธ์Œ
                break;
            }
        }

        if (position != -1) {
            System.out.println(target + " ์˜ ์œ„์น˜๋Š” ๋ฐฐ์—ด์˜ "+ position + " ๋ฒˆ ์ธ๋ฑ์Šค" );
        }else {
            System.out.println(target + " ์ด ๋ฐฐ์—ด์— ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
        }


        scanner.close();
    }
}
728x90