์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- ๋ฐ์ดํฐ ์ ์ก
- ๋น์ฃผ๊ธฐ์ ํธ
- ์ฝ๋ฉํ ์คํธ์ค๋น
- ํ๋ก์ด๋์์
- xv6
- ๊ฐ๋ฐ์์ทจ์
- ์ฃผ๊ธฐ์ ํธ
- tcp ์ธ๊ทธ๋จผํธ
- ์ฐ๋ถํฌdb
- IEEE 802
- git merge
- ์ค๋ธ์
- well known ํฌํธ
- ํ ํฐ ๋ฒ์ค
- ํญํด99
- ์ค๋ฅ์ ์ด
- ์ค๋ฅ๊ฒ์ถ
- 99ํด๋ฝ
- mariadb
- ํ๋ ์ ๊ตฌ์กฐ
- reducible
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ์๋น์ค ํ๋ฆฌ๋ฏธํฐ๋ธ
- i-type
- ์ค๋ ๋
- ์์๋ฒํธ
- tcp ํ๋กํ ์ฝ
- ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ
- leetcode
- til
- Today
- Total
Unfazedโ๏ธ๐ฏ
๋ฌธ์์ด.repeat() ๋ฌธ์์ด ๋ฐ๋ณต ๋ฉ์๋ (java.lang.String) +๋ด๋ถ ์์ค์ฝ๋ ๋ถ์ ๋ณธ๋ฌธ
๋ฌธ์์ด.repeat() ๋ฌธ์์ด ๋ฐ๋ณต ๋ฉ์๋ (java.lang.String) +๋ด๋ถ ์์ค์ฝ๋ ๋ถ์
9taetae9 2023. 10. 27. 19:36public String repeatโ(int count)
If this string is empty or count is zero then the empty string is returned.
Parameters:count - number of times to repeatReturns:A string composed of this string repeated count times or the empty string if this string is empty or count is zeroThrows:IllegalArgumentException - if the count is negative.Since:11
Java์์๋ "String' ํด๋์ค์ repeat(int count) ๋ฉ์๋๊ฐ ์๋ค. Java11๋ถํฐ ๋์ ๋ ๊ธฐ๋ฅ์ผ๋ก 'repeat(int count)' ๋ฉ์๋๋ ํด๋น ๋ฌธ์์ด์ ์ฃผ์ด์ง 'count' ํ์๋งํผ ๋ฐ๋ณตํ์ฌ ์๋ก์ด ๋ฌธ์์ด์ ์์ฑํ๋ค.
Ex)
String str = "-";
System.out.println(str.repeat(5)); // ์ถ๋ ฅ: -----
repeat(int count) ๋ฉ์๋๋ ๋ด๋ถ์ ์ผ๋ก Arrays.copyOf๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด์ ๋ฐ๋ณตํ๋ค๊ณ ํ๋ค. 'Arrays.copyOf'์ ์๋ ์๋ฆฌ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐฐ์ด์ ๋ณต์ฌํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ ํ ์๊ฐ ๋ณต์ก๋, ์ฆ O(n)์ ๊ฐ์ง๋ค. ์ฌ๊ธฐ์ n์ ๋ณต์ฌํ๋ ค๋ ๋ฐฐ์ด์ ๊ธธ์ด๋ผ ์๊ฐํ๋ฉด๋๋ค.
String.repeat(int count)๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ์๋ณธ String์ ๊ธธ์ด๋ฅผ l, ๋ฐ๋ณต ํ์๋ฅผ count๋ผํ๋ฉด, ๊ฒฐ๊ณผ์ ์ ์์ฑ๋๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ l*count๊ฐ ๋๋ค. ๋ฐ๋ผ์ ์๊ฐ ๋ณต์ก๋๋ O(l*c)์ด๋ค.
๋ด๋ถ ์์ค ์ฝ๋๋ฅผ ์ดํด๋ณด์.
public String repeat(int count) {
if (count < 0) { // 'count' ๊ฐ์ด ์์์ผ ๊ฒฝ์ฐ 'IllegalArgumentException'์ ๋ฐ์์ํจ๋ค.
throw new IllegalArgumentException("count is negative: " + count);
}
if (count == 1) { //'count' ๊ฐ์ด 1์ธ ๊ฒฝ์ฐ ๋ฌธ์์ด์ ๋ณต์ฌ ์์ด ์๋ ๋ฌธ์์ด์ ๋ฐํํ๋ค.
return this;
}
final int len = value.length; //'value'๋ ์๋ณธ ๋ฌธ์์ด์ ๋ด๋ถ 'byte' ๋ฐฐ์ด
if (len == 0 || count == 0) { //ํด๋น ๋ฌธ์์ด์ด "" ๋๋ count๊ฐ 0์ผ ๊ฒฝ์ฐ ""๋ฅผ ๋ฐํํ๋ค.
return "";
}
// ๋ฉ๋ชจ๋ฆฌ ์ ํ ํ์ธ. ๊ฒฐ๊ณผ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 'int'์ ์ต๋ ํฌ๊ธฐ๋ฅผ ์ด๊ณผํ๋์ง ํ์ธ
//์ด๊ณผํ ๊ฒฝ์ฐ 'OutOfMemoryError'๋ฅผ ๋ฐ์์ํจ๋ค.
if (Integer.MAX_VALUE / count < len) { // Integer์ ์ต๋๊ฐ < len*count(์ด ๊ธธ์ด)
throw new OutOfMemoryError("Required length exceeds implementation limit");
}
if (len == 1) { //๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 1์ผ ๊ฒฝ์ฐ
final byte[] single = new byte[count];
Arrays.fill(single, value[0]); //ํด๋น๋ฌธ์๋ฅผ count๋งํผ ๋ฐ๋ณตํ์ฌ ์๋ก์ด ๋ฐฐ์ด ์์ฑ
return new String(single, coder); //coder๋ ์๋ ๋ณ๋๋ก ์ค๋ช
.
}
// ๋ ์ด์์ ๋ฌธ์๋ก ๊ตฌ์ฑ๋ ์ผ๋ฐ์ ๊ฒฝ์ฐ
final int limit = len * count; // limit =๋ฌธ์์ด ๊ธธ์ด * ๋ฐ๋ณต ํ์
final byte[] multiple = new byte[limit]; // ๊ฒฐ๊ณผ ๋ฌธ์์ด์ ๋ด๊ธฐ ์ํ limit์ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง ๋ฐฐ์ด
System.arraycopy(value, 0, multiple, 0, len); //'value'๋ฐฐ์ด์ '0'๋ฒ ์ธ๋ฑ์ค๋ถํฐ 'len'์ ๊ธธ์ด๋งํผ์ ๋ฐ์ดํฐ๋ฅผ 'multiple' ๋ฐฐ์ด์ ์ฒ์๋ถํฐ ์์ํด์ ๋ณต์ฌํ๋ค. ๋ฐ๋ผ์ ์๋ณธ ๋ฌธ์์ด์ 'byte' ๋ฐ์ดํฐ๊ฐ 'multiple'๋ฐฐ์ด์ ์ ๋ถ๋ถ์ ๋ณต์ฌ๋ ์ํฉ์ด๋ค.
repeatCopyRest(multiple, 0, limit, len); //'multiple' ๋ฐฐ์ด ์๋ถ๋ถ์ ๋ณต์ฌ๋ ์๋ณธ ๋ฌธ์์ด์ ๋๋จธ์ง ๋ถ๋ถ์๋ ๋์ผํ๊ฒ ๋ฐ๋ณตํด์ ๋ณต์ฌํ๋ค.
return new String(multiple, coder);
}
'coder'๋ 'String' ํด๋์ค์ ๋ด๋ถ ๊ตฌํ์ ์ผ๋ถ๋ก ์ฌ์ฉ๋๋ ๋ณ์๋ก์จ, Java9 ๋ถํฐ 'String' ํด๋์ค๋ ๋ฌธ์์ด์ ํํํ๊ธฐ ์ํด char ๋ฐฐ์ด๋์ byte ๋ฐฐ์ด('value[]')๋ฅผ ์ฌ์ฉํ๊ฒ ๋์๋ค๊ณ ํ๋ค. ๊ทธ๋ฌ๋ byte ๋ฐฐ์ด์ ์ฌ์ฉํ๋ฉด ๋ฌธ์์ด์ด ISO-8859-1 ๋๋ UTF-16 ์ค ์ด๋ ์ธ์ฝ๋ฉ์ผ๋ก ์ ์ฅ๋์๋์ง๋ฅผ ์์์ผํ๋๋ฐ, ์ด๋ฅผ ์ํด 'coder'๋ผ๋ ๋ณ์๊ฐ ๋์ ๋์๋ค.
'coder'๋ ๋ ๊ฐ์ง ๊ฐ์ ๊ฐ์ง๋๋ฐ
1. LATIN1 : ISO-8859-1 ๋๋ Latin-1 ์ธ์ฝ๋ฉ์ ๋ํ๋ธ๋ค. ์ด๋ ๊ฐ ๋ฌธ์๋ฅผ ํ๋์ ๋ฐ์ดํธ๋ก ํํํ๋ ์ธ์ฝ๋ฉ์ด๋ค.
2. UTF16 : UTF-16 ์ธ์ฝ๋ฉ์ ๋ํ๋ธ๋ค. ๋ฌธ์์ด ๋ด์ ๋ฌธ์๋ง๋ค ๋ ๋ฐ์ดํธ ์ด์์ ์ฌ์ฉํ ์ ์๋ค.
'coder'์ ์ฌ์ฉ ๋ฐฐ๊ฒฝ์ ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ์ ๊ด๋ จ๋์ด ์๋ค.
๋ง์ ๋ฌธ์์ด์ด ASCII ๋ฌธ์๋ Latin-1๋ฌธ์๋ง ํฌํจํ๊ณ ์๊ธฐ ๋๋ฌธ์, ๋ฌธ์์ด์ byte๋ก ํํํ๋ฉด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ์ฝํ ์ ์๋ค๊ณ ํ๋ค. UTF-16 ๋ฌธ์์ด์ ๊ฒฝ์ฐ 'coder'๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ์ธ์ฝ๋ฉ์ ๋ํ๋ธ๋ค.
๋ฐ๋ผ์, 'coder'๋ 'String' ํด๋์ค๊ฐ ๋ด๋ถ์ ์ผ๋ก ์ด๋ค ๋ฐฉ์์ผ๋ก ๋ฌธ์์ด์ ์ธ์ฝ๋ฉํ์ฌ ์ ์ฅํ๊ณ ์๋์ง ๋ํ๋ด๋ ์ญํ ์ ํ๋ค.
์ฐธ๊ณ : https://medium.com/@ggajos/java-11-how-string-repeat-was-implemented-and-why-d93796b7abba
Implementing JDK String.repeat in Java 11
Since Java 11. There is a new method in the String class called repeat. Yes, it repeats string, fast. Now guess. How many implementation…
medium.com
์์ค ์ฝ๋: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/String.java
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ํจ์จ์ ์ธ ๋ฌธ์์ด ๊ฒ์ฌ: isEmpty()์ isBlank() ์ฌ์ฉํ๊ธฐ (0) | 2024.09.07 |
---|---|
Collection.toArray(new T[0]) vs .toArray(new T[size]) / ์ปฌ๋ ์ ์ ๋ฐฐ์ด๋ก ๋ณํํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์ฑ๋ฅ ๋น๊ต (0) | 2024.01.14 |
๋ฉ์๋ ์ฐธ์กฐ(method reference) (0) | 2023.10.06 |
์ปฌ๋ ์ ํ๋ ์์๊ณผ ํจ์ํ ์ธํฐํ์ด์ค (0) | 2023.10.06 |
Predicate์ ๊ฒฐํฉ (0) | 2023.10.06 |