πŸ‘¨‍πŸ’» Web Development/Java

[Day10] Java 10 - μ§„μˆ˜ λ³€ν™˜

Kim_dev 2022. 9. 1. 20:39

[Day10] Java 10 [8/22]

 

1.  String 클래슀 ν•¨μˆ˜(λ©”μ„œλ“œ) 

charAt(index)
equals()
split(regex)
matches(regex)
substring()
trim()
valueOf()
format()
join()

2.  10μ§„μˆ˜ 2μ§„μˆ˜ μ „ν™˜

// [문제] 10μ§„μˆ˜ μ •μˆ˜λ₯Ό μž…λ ₯λ°›μ•„μ„œ
		//   2λ°”μ΄νŠΈ 2μ§„μˆ˜ν˜•νƒœλ‘œ 좜λ ₯ν•˜μ„Έμš”.
		// 예) 10
		//     0000 0000    0000 1010 
		Scanner scanner = new Scanner(System.in);
		System.out.print("> 10μ§„μˆ˜ μ •μˆ˜ μž…λ ₯ ? ");
		int n = scanner.nextInt();
		 
		int [] binaryArray = new int[16];

		int index = binaryArray.length - 1;
		
		// 4:10 풀이 μ‹œμž‘
		// n = 10
		int share = n, rest ;
		
		/*
		share = share /2;  // 5
		rest = share % 2;    // 0
		binaryArray[ index ] = rest;
		index--;
		
		share = share /2;  //      2
		rest = share % 2;    //        1
		binaryArray[ index ] = rest;
		index--;
		
		share = share /2;  //      1
		rest = share % 2;    //   0
		binaryArray[ index ] = rest;
		index--;
		
		:
		:
		*/
		// 쀑단점(break point)               F11  디버깅~
		while(  share != 0 ) {   
			rest = share % 2;    
			share = share /2;   
			binaryArray[ index-- ] = rest;   
		}
		
		// int [] 배열은 μ΄ˆκΈ°ν™” ν•˜μ§€ μ•ŠμœΌλ©΄ int의 기본값인 0으둜 μ΄ˆκΈ°ν™” λ˜μ–΄μ Έ μžˆλ‹€...( μ•”κΈ° )
		// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
		System.out.println(   Arrays.toString(  binaryArray )  );
		// 00000000000010
		String result =  Arrays.toString(  binaryArray ) ;
		result =  result.replaceAll(",\\s", "");
		System.out.println( result.substring(1, result.length()-1) );
String binaryN = Integer.toBinaryString( n )