質問編集履歴
4
解決
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
ぶじかいけつしました
|
test
CHANGED
@@ -1,101 +1,7 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
### 発生している問題・エラーメッセージ
|
4
4
|
|
5
|
-
|
5
|
+
### 該当のソースコード
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
###
|
7
|
+
### 補足
|
10
|
-
|
11
|
-
```
|
12
|
-
|
13
|
-
class Sort {
|
14
|
-
|
15
|
-
protected String[] ary = { "abc", "bac", "cab","123","231" };
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
public int getIdx() {
|
20
|
-
|
21
|
-
return ary.length;
|
22
|
-
|
23
|
-
}
|
24
|
-
|
25
|
-
public String[] getAry() {
|
26
|
-
|
27
|
-
return ary;
|
28
|
-
|
29
|
-
}
|
30
|
-
|
31
|
-
public void asc() {
|
32
|
-
|
33
|
-
for (int i = 0; i < ary.length; i++) {
|
34
|
-
|
35
|
-
for (int j = 1; j < ary.length; j++) {
|
36
|
-
|
37
|
-
if ((int) ary[j].charAt(j-j) > (int) ary[i].charAt(i)) {
|
38
|
-
|
39
|
-
String tmp = ary[i];
|
40
|
-
|
41
|
-
ary[i] = ary[j];
|
42
|
-
|
43
|
-
ary[j] = tmp;
|
44
|
-
|
45
|
-
}
|
46
|
-
|
47
|
-
}
|
48
|
-
|
49
|
-
}
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
```
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
```
|
60
|
-
|
61
|
-
class Sample {
|
62
|
-
|
63
|
-
public static void main(String[] args) {
|
64
|
-
|
65
|
-
Sort s = new Sort();
|
66
|
-
|
67
|
-
s.asc();
|
68
|
-
|
69
|
-
s.getAry();
|
70
|
-
|
71
|
-
for (int i = 0; i < s.getIdx(); i++) {
|
72
|
-
|
73
|
-
System.out.println(s.getAry()[i]);
|
74
|
-
|
75
|
-
}
|
76
|
-
|
77
|
-
}
|
78
|
-
|
79
|
-
}
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
```
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
### 試したこと
|
88
|
-
|
89
|
-
文字列同士を比べる際にまずはstring→charに変換し、文字列コードで比較すると思うのですが、
|
90
|
-
|
91
|
-
この手順だけで比較するとString型配列とchar型配列の違いでエラーが出てしまいます。
|
92
|
-
|
93
|
-
```
|
94
|
-
|
95
|
-
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
|
96
|
-
|
97
|
-
String index out of range: 1
|
98
|
-
|
99
|
-
```
|
100
|
-
|
101
|
-
上記手順で抜けている点のアドバイスをお願いいたします。
|
3
コードの編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
class Sort {
|
14
14
|
|
15
|
-
protected String[] ary = { "b", "a", "c" };
|
15
|
+
protected String[] ary = { "abc", "bac", "cab","123","231" };
|
16
16
|
|
17
17
|
|
18
18
|
|
2
コードの編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,17 +32,15 @@
|
|
32
32
|
|
33
33
|
for (int i = 0; i < ary.length; i++) {
|
34
34
|
|
35
|
-
for (int j =
|
35
|
+
for (int j = 1; j < ary.length; j++) {
|
36
36
|
|
37
|
-
if ((int) ary[j].charAt(j) > (int) ary[i].charAt(i)) {
|
37
|
+
if ((int) ary[j].charAt(j-j) > (int) ary[i].charAt(i)) {
|
38
38
|
|
39
39
|
String tmp = ary[i];
|
40
40
|
|
41
41
|
ary[i] = ary[j];
|
42
42
|
|
43
43
|
ary[j] = tmp;
|
44
|
-
|
45
|
-
return;
|
46
44
|
|
47
45
|
}
|
48
46
|
|
1
コードの編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,6 +3,86 @@
|
|
3
3
|
配列内文字列をソートしたい。
|
4
4
|
|
5
5
|
importは使わず。(eg. arraylist)
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
### 該当コード
|
10
|
+
|
11
|
+
```
|
12
|
+
|
13
|
+
class Sort {
|
14
|
+
|
15
|
+
protected String[] ary = { "b", "a", "c" };
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
public int getIdx() {
|
20
|
+
|
21
|
+
return ary.length;
|
22
|
+
|
23
|
+
}
|
24
|
+
|
25
|
+
public String[] getAry() {
|
26
|
+
|
27
|
+
return ary;
|
28
|
+
|
29
|
+
}
|
30
|
+
|
31
|
+
public void asc() {
|
32
|
+
|
33
|
+
for (int i = 0; i < ary.length; i++) {
|
34
|
+
|
35
|
+
for (int j = 0; j < ary.length; j++) {
|
36
|
+
|
37
|
+
if ((int) ary[j].charAt(j) > (int) ary[i].charAt(i)) {
|
38
|
+
|
39
|
+
String tmp = ary[i];
|
40
|
+
|
41
|
+
ary[i] = ary[j];
|
42
|
+
|
43
|
+
ary[j] = tmp;
|
44
|
+
|
45
|
+
return;
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
class Sample {
|
64
|
+
|
65
|
+
public static void main(String[] args) {
|
66
|
+
|
67
|
+
Sort s = new Sort();
|
68
|
+
|
69
|
+
s.asc();
|
70
|
+
|
71
|
+
s.getAry();
|
72
|
+
|
73
|
+
for (int i = 0; i < s.getIdx(); i++) {
|
74
|
+
|
75
|
+
System.out.println(s.getAry()[i]);
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
```
|
6
86
|
|
7
87
|
|
8
88
|
|
@@ -16,7 +96,7 @@
|
|
16
96
|
|
17
97
|
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
|
18
98
|
|
19
|
-
String index out of range:
|
99
|
+
String index out of range: 1
|
20
100
|
|
21
101
|
```
|
22
102
|
|