//split 해서 각 행에 각 자리의 숫자 집어넣기. (숫자가 없는 곳은 -1을 집어넣음)
/*
자리수 체크하는 코드
public static int check(int x){
return (int)(Math.log10(x)+1);
}
*/
for(int j=0;j<numbers.length;j++){
temp=v[j][0];
cases=check(temp);
switch(cases){
case 1 : //1의 자리
v[j][1]=temp;
v[j][2]=-1;
v[j][3]=-1;
break;
case 2 : //10의 자리
a=Integer.toString(temp);
v[j][1]=a.charAt(0)-'0';
v[j][2]=a.charAt(1)-'0';
v[j][3]=-1;
break;
case 3: //100의 자리
a=Integer.toString(temp);
v[j][1]=a.charAt(0)-'0';
v[j][2]=a.charAt(1)-'0';
v[j][3]=a.charAt(2)-'0';
break;
}
}
그 대신 sort 할 때 행이 다 같이 움직여야 함. (comparator를 이용함)
- 정렬하는 부분 코드
public static void ArraysSort2(int[][] arr, final int index_num, int from, int to){
Arrays.sort(arr,from,to+1, new Comparator<int[]>(){
public int compare(final int[] entry1, final int[] entry2){
final int time1=entry1[index_num];
final int time2=entry2[index_num];
return Integer.compare(time2,time1);
}
});
}
매개 변수 설명: ArraysSort2(배열 이름, 몇 열을 정렬할 건지, 몇 행부터, 몇 행까지 정렬할 건지)
from과 to 가 있는 이유) 33, 35, 9를 정렬해야 할 때 첫 자릿수가 '3'으로 같은 애들만 비교하기 위해 from 과 to를 설정함
- 앞 자리수가 같은 경우의 행을 반환(30과 34의 경우)하는 코드
그럼 위 예제처럼 앞 자릿수가 '3'으로 같은지 비교하는 메서드는 아래와 같음. from과 to를 배열에 저장해 반환
public static int[] isSame(int[][] a, int x){
int[] from_to = new int[a.length];
int from=0, to=0;
int pivot=a[0][x];
for(int i=1;i<a.length;i++){
if(pivot==a[i][x]){
to++;
}
else{
from=i;
pivot=a[i][x];
to=i;
}
}
from_to[0]=from;
from_to[1]=to;
return from_to;
}
1. sort(), comparator, comparable를 사용한다는 것까진 왔으나, 이에 대해 깊이/이해하지 않고, 더 어렵게 생각해서 풂.
2. 그리고 string형 합칠 때 +가 아닌, String.valueOf()를 사용하는 게 더 효율적이라는 것을 깨달음
"java convert int to string"
char c = 'a';
String s = String.valueOf(c); // fastest + memory efficient
String s = Character.toString(c);
String s = new String(new char[]{c});
String s = String.valueOf(new char[]{c});
String s = new Character(c).toString();
String s = "" + c; // slowest + memory inefficient