//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){
case1 : //1의 자리
v[j][1]=temp;
v[j][2]=-1;
v[j][3]=-1;
break;
case2 : //10의 자리
a=Integer.toString(temp);
v[j][1]=a.charAt(0)-'0';
v[j][2]=a.charAt(1)-'0';
v[j][3]=-1;
break;
case3: //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를 이용함)
- 정렬하는 부분 코드
publicstaticvoidArraysSort2(int[][] arr, finalint index_num, int from, int to){
Arrays.sort(arr,from,to+1, new Comparator<int[]>(){
publicintcompare(finalint[] entry1, finalint[] entry2){
finalint time1=entry1[index_num];
finalint time2=entry2[index_num];
return Integer.compare(time2,time1);
}
});
}
매개 변수 설명: ArraysSort2(배열 이름, 몇 열을 정렬할 건지, 몇 행부터, 몇 행까지 정렬할 건지)
from과 to 가 있는 이유) 33, 35, 9를 정렬해야 할 때 첫 자릿수가 '3'으로 같은 애들만 비교하기 위해 from 과 to를 설정함
- 앞 자리수가 같은 경우의 행을 반환(30과 34의 경우)하는 코드
그럼 위 예제처럼 앞 자릿수가 '3'으로 같은지 비교하는 메서드는 아래와 같음. from과 to를 배열에 저장해 반환
publicstaticint[] isSame(int[][] a, int x){
int[] from_to = newint[a.length];
intfrom=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;
}
Comments