728x90
๋ฐ˜์‘ํ˜•

์ด๋ฒˆ์ฃผ ์ž๋ฐ” ๋น„์ „๊ณต ๊ฐ•์˜ + ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ ํ’€๋ฉด์„œ ํ—ท๊ฐˆ๋ฆฌ๊ณ  ์ž˜ ๋ชจ๋ฅด๋Š”๊ฒƒ๋“ค ์ •๋ฆฌ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค๐Ÿ˜‚๐Ÿ˜‚

 

1. ๋ฌธ์ž์—ด ์ •์ˆ˜ ๋ณ€ํ™˜ (ํŒŒ์ด์ฌ์€ ์ •๋ง ๊ฐ„๋‹จํ•œ๋ฐ์š”...ใ…œใ…œ ์‚ฌ์‹ค ์ž๋ฐ”๋„ ๊ทธ๋ฆฌ ๋ณต์žกํ•˜์ง„ ์•Š์Œ )

  • Interger.valueOf() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ 
// ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค. ์ด์–ด๋ถ™์ธ ์ˆ˜
// new_์ •์ˆ˜ = Integer.valueOf(๋ฌธ์ž์—ด) 

class Solution {
    public int solution(int[] num_list) {
        int answer = 0;
        String even = "";
        String odd = "";
        for (int i = 0; i < num_list.length; i ++){
            if (num_list[i] %2 == 1) {
                even += num_list[i];
            }else {
                odd += num_list[i];
            }
        }
        answer += Integer.valueOf(even) + Integer.valueOf(odd);
        return answer;
    }
}
  • Integer.parseInt() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
  String str ="30";

  int i = Integer.parseInt(str);  

  System.out.println(i); // 30


๋‘˜์˜  ์ฐจ์ด์ ์€...
valueOf() ๋ฉ”์†Œ๋“œ๋Š” ๋ฌธ์ž์—ด์˜ ๊ฐ’์„ ์ •์ˆ˜ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค ์ฐธ์กฐ ์ž๋ฃŒํ˜•(Reference Type)์ธ Integer ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์„œ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
parseInt() ๋ฉ”์†Œ๋“œ๋Š” ๊ฒฐ๊ณผ๊ฐ’์„ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•(Primitive Type)์ธ int ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ  ์‹ถ์„ ๋•Œ๋Š” parseInt() ๋ฅผ, ์ฐธ์กฐ ์ž๋ฃŒํ˜• Integer ๊ฐ์ฒด๋กœ ๋ฐ›์•„์˜ค๊ณ  ์‹ถ์„ ๋•Œ๋Š” valueOf() ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค

์•„์ง ๊ฐ์ฒด๋ฅผ ๋ชป ๋ฐฐ์›Œ์„œ ๊ทธ๋Ÿฐ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์ •์ง„... ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ์•„์‹œ๋Š” ์ž๋ฐ” ๊ณ ์ˆ˜๋‹˜ ๋œ ์ข€ ์•Œ๋ ค์ฃผ์„ธ์š”...

 

 

2. ์ •์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜(๋งŽ์Œ ์ฃผ์˜...;;;;!!!)

  • Integer.toString()
// 1. Integer.toString() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ 
// ๋ฌธ์ž์—ด = Integer.toString(์ •์ˆ˜)

public class IntToString {
    public static void main(String[] args) {
        int intValue1 = 0812;
        int intValue2 = 8888;
 
        String str1 = Integer.toString(intValue1);
        String str2 = Integer.toString(intValue2);
 
        System.out.println(str1);
        System.out.println(str2);
    }
}

 

  • String.valueOf()
// 2. String.valueOf()
// ๋ฌธ์ž์—ด = String.valueOf(์ •์ˆ˜)

public class IntToString {
    public static void main(String[] args) {
        int intValue1 = 0812;
        int intValue2 = 8888;
 
        String str1 = Integer.valueOf(intValue1);
        String str2 = Integer.valueOf(intValue2);
 
        System.out.println(str1);
        System.out.println(str2);
    }
}

 

  • int + ""
// 3. int + "" (์ œ์ผ ๊ฐ„๋‹จํ•˜๊ณ  ์ข‹๋„ค์š”^^"")
// ๋ฌธ์ž์—ด : int + ""

public class IntToString {
    public static void main(String[] args) {
        int intValue1 = 0812;
        int intValue2 = 8888;
 
        String str1 = intValue1 + "";
        String str2 = intValue2 + "";
 
        System.out.println(str1);
        System.out.println(str2);
    }
}

 

3. for each(๊ฐœ์„ ๋œ for ๋ฌธ)

 

: ์žฅ์  : ๊ฐ„ํŽธ์„ฑ, ๊ฐ€๋…์„ฑ ์ข‹์€ ์ฝ”๋“œ... ๋ฐฐ์—ด ์ธ๋ฑ์Šค ๋ฌธ์ œ ํ•ด๊ฒฐ

: ๋‹จ์  : ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค.. ์ˆ˜์ • ๋…ธ์šฐ ใ…œ

 

// for each

// ๋™์ž‘
for(์ž๋ฃŒํ˜• ๋ณ€์ˆ˜๋ช… : ๋ฐฐ์—ด๋ช…) {
	๋ฌธ์žฅ
}

// ์˜ˆ์‹œ
int nums = [1, 2, 3, 4]
for (int num : nums) {
	System.out.println(num);
}
// 1
// 2
// 3
// 4

์–ธ์  ๊ฐ€ for๋ฌธ ์„ ์“ฐ๋Š”๊ฒŒ ๋Šฅ์ˆ™ํ•ด์ง€๋ฉด ์ด ๋ฐฉ๋ฒ•์œผ๋กœ๋„ ์จ๋ด์•ผ๊ฒ ๋‹ค ๐Ÿ˜Š

 

4. charAt() ๋ฉ”์„œ๋“œ

 

: ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ!!

// .charAt()
// .charAt(์ธ๋ฑ์Šค)

String str = "์•ˆ๋…•ํ•˜์„ธ์šฐ";
char ch1 = str.charAt(0);
char ch2 = str.charAt(1);
char ch3 = str.charAt(2);

System.out.println(ch1); // ์•ˆ
System.out.println(ch2); // ๋…•
System.out.println(ch3); // ํ•˜

 

5. == ๊ณผ .euqlas()์˜ ์ฐจ์ด

 

  •  == ์—ฐ์‚ฐ์ž : ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๋‘๊ฐœ์˜ ๋Œ€์ƒ์˜ ์ฃผ์†Œ๊ฐ’์„ ๋น„๊ต
  • equals ๋ฉ”์†Œ๋“œ : ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๋‘๊ฐœ์˜ ๋Œ€์ƒ์˜ ๊ฐ’ ์ž์ฒด๋ฅผ ๋น„๊ต

์•„์ง ํด๋ž˜์Šค, ์ƒ์† ํŒŒํŠธ๋ฅผ ์•ˆ ๋ฐฐ์›Œ์„œ ์ •ํ™•ํ•œ ์ฐจ์ด ๊ตฌ๋ณ„์ด ์•ˆ๋œ๋‹ค...

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค๊ฐ€ ==์œผ๋กœ ์•„๋ฌด๋ฆฌ ํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์•ˆ ํ’€๋ฆฌ๊ธธ๋ž˜ ๊ตฌ๊ธ€๋ง์„ ํ–ˆ๋Š”๋ฐ ๋‹ค๋“ค equals๋ฅผ ์‚ฌ์šฉํ•˜์…จ๋‹ค..

equals()๊ฐ€ ๊ฐ’์„ ๋น„๊ตํ•˜๋‹ˆ๊นŒ ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ๋•Œ ๋งŒํผ์€ == ์—ฐ์‚ฐ์ž๋ณด๋‹ค๋Š” ๋‚˜์„๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค ์ผ๋‹จ์€.. 

๋‹ค๋ฅธ๋ถ„ ๋ธ”๋กœ๊ทธ์—์„œ ํผ์™”๋‹ค... ์ฃผ์†Œ ๋‚จ๊น๋‹ˆ๋‹ค...

https://sudo-minz.tistory.com/93

 

[Java] ์ž๋ฐ” ๋ฌธ์ž์—ด ๋น„๊ต == equals() ์ฐจ์ด์ 

Java์—์„œ int์™€ boolean๊ณผ ๊ฐ™์€ ์ผ๋ฐ˜์ ์ธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๋น„๊ต๋Š” ==์ด๋ผ๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๊ตํ•œ๋‹ค. ํ•˜์ง€๋งŒ String์ฒ˜๋Ÿผ Class์˜ ๊ฐ’์„ ๋น„๊ตํ• ๋•Œ๋Š” == ๋Œ€์‹  equals()๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋น„๊ตํ•˜๋Š”๋ฐ ์–ด๋–จ

sudo-minz.tistory.com

 

6. ์ž๋ฐ” ๋ฌธ์ž์—ด ํ•œ๊ธ€์ž์”ฉ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ธฐ

// Split ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹จ์–ด๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ธฐ

class Solution {
    public int solution(String number) {
        int answer = 0;
        int sum_number = 0;
        
        String[] nums;                // String์„ ๋‹ด์„ ๋ฐฐ์—ด ๋งŒ๋“ค์–ด์ฃผ๊ธฐ
        nums = number.split("");      // ๋ฐฐ์—ด์— ํ•œ๊ธ€์ž์”ฉ ์ €์žฅํ•˜๊ธฐ
        for (int i = 0; i < number.length(); i++){ // for๋ฌธ์œผ๋กœ ํ•˜๋‚˜์”ฉ ๋Œ๋ฆฌ๋ฉด์„œ ๊ฐ’ ์ €์žฅํ•˜๊ธฐ
            sum_number += Integer.parseInt(nums[i]);
        }
        answer = sum_number % 9;
        return answer;
    }
}


// charAt ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‹จ์–ด๋ฅผ char ๋ฐฐ์—ด์— ํ•œ๊ธ€์ž์”ฉ ์ €์žฅํ•˜๊ธฐ

char[] num = new char[number.length()]; // String์„ ๋‹ด์„ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
for (int i = 0; i < num.length; i++) {
	num[i] = numver.charAt(i);
}

 

7. ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ(+์—ฐ์‚ฐ์ด ๋งŽ์•„์ง„๋‹ค๋ฉด ์„ฑ๋Šฅ์ ์œผ๋กœ ์ข‹์ง€ ์•Š๋‹ค) => StringBuilder

 

// ๋ฌธ์ž์—ด ๋งŒ๋“œ๋ฆญ :StringBuilder

public class Main {
	public static void main(String[] args) {
    	StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append('๋ฌธ์ž์—ด").append("์—ฐ๊ฒฐ"); // ํ•œ๋ฒˆ์— ๋‘๊ฐœ append ๊ฐ€๋Šฅ
        // String str = stringBuilder; // String์— StringBuilder๋ฅผ ๊ทธ๋Œ€๋กœ ๋„ฃ์„ ์ˆ˜๋Š” ์—†์€  toString()์„ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค
        String str = stringBuilder.toString();
        System.out.println(stringBuilder); // ๋ฌธ์ž์—ด์—ฐ๊ฒฐ
        System.out.println(str); // ๋ฌธ์ž์—ด์—ฐ๊ฒฐ
    }
}
728x90
๋ฐ˜์‘ํ˜•

+ Recent posts