결과 집합의 값을 보유하는 배열이 있습니다. 배열을 검색하는 코드의 개요는 다음과 같습니다.
public String[][] ref_Details() {
int i = 0;
String a[][] = new String[47][11];
try
{
con = getConnection();
stmt = con.createStatement();
String sql=" select b.LOGTIME, b.beam_current, b.beam_energy ..."; // shortened
stmt.executeQuery(sql);
rs = stmt.getResultSet();
while(rs.next()) {
for(int j=0; j<11; j++)
a[i][j] = rs.getString(j+1);
i++;
}
}
catch( Exception e ) { ... }
finally {
closeConnection(stmt, rs, con);
}
return a;
}
얻은 샘플 테이블은 다음과 같습니다.
표에서 두 번째 열에 beam_current
는 0에서 220까지의 각 정수 배수에 가까운 값이 있음이 분명합니다 . (0, 10, 20 ... 220). 10의 배수마다 그 배수에 가장 가까운 행만 선택하도록 데이터 세트를 필터링하고 싶습니다. 이를 위해 나는 :
beam_current
얻은 차이를 찾으십시오. 차이가 가장 작은 행이 10의 배수에 관심이있는 유일한 행입니다. T내 예상 결과는 샘플 데이터의 원래 47 행 대신 22 행입니다. 예를 들어 위의 샘플 데이터 그림에서 번호가 21 인 행은 값에 대해 선택된 행에 해당합니다 130
.
내 문제는 내가 예상 한 결과를 볼 수 없다는 것입니다. 내가 시도한 코드는 다음과 같습니다.
public int[] ref_BeamCurrent() {
int i = 0;
int[] arr = new int[47];
try
{
con = getConnection();
...
rs = stmt.getResultSet();
while(rs.next())
{
for(i=0; i<arr.length; i++)
{
arr[i] = rs.getInt(2);
System.out.println(arr);
while (i < arr.length && number <= 210)
{
arr[i] = arr[i] - number;
System.out.println(arr);
number = number + 10;
System.out.println(number);
i = i + 1;
// System.out.println(arr);
}
}
}
}
catch( Exception e ) { ... }
finally { ... }
return arr;
}
이 코드는 완전히 잘못된 행을 선택하는 것 같습니다. 내가 뭘 잘못하고 있습니까?
다른 방식으로 생각해야한다고 생각합니다. 당신이 원하는 것은 각 행에 대해 10의 배수에서 거리를 찾는 것입니다.
double mult = 10d * Math.round(v / 10d)
double delta = Math.abs(v - mult)
mult
대해 원하는 행은 가장 작은 delta
값을 가진 행 입니다.따라서 행을 한 번만 반복하면됩니다.
beam_value
행과 그것의 발견 mult
과 delta
.delta
가까운 이전에 발견 된 것보다 delta
위해 mult
, 그 다음에 그 행을 기록 mult
, 그렇지 않으면 무시한다.또한이 접근 방식은 다른 접근 방식으로는 방지하기 어려운 10의 배수 이상에 대해 단일 행이 기록되는 것을 방지합니다.
예를 들어 (그리고 SQL 쿼리가 없기 때문에 데이터를 가짜로 만들었습니다). 입력 데이터 :
`0.5, 12.10, 13.00, 16.01, 21.52`
올바른 출력을 제공합니다 (인덱스 1은 10
인덱스 2보다 더 가깝고 인덱스 4는 20
인덱스 3보다 더 가깝습니다 ).
10x 행 값 0 0 0.5000 10 1 12.1000 20 4 21.5200
코드 :
public static void findClosestRowsToMultiplesOfTen() {
// fake row values
double[] vals = new double[]{ 0.5, 12.10, 13.00, 16.01, 21.52 };
// get the max value, and its multiple of ten to get the number of buckets
double max = Double.MIN_VALUE;
for (double v : vals) max = Math.max(max, v);
int bucketCount = 1 + (int)(max/10);
// initialise the buckets array to store the closest values
double[][] buckets = new double[bucketCount][3];
for (int i = 0; i < bucketCount; i++){
// store the current smallest delta in the first element
buckets[i][0] = Double.MAX_VALUE;
// store the current "closest" index in the second element
buckets[i][1] = -1d;
// store the current "closest" value in the third element
buckets[i][2] = Double.MAX_VALUE;
}
// iterate the rows
for (int i = 0; i < vals.length; i++)
{
// get the value from the row
double v = vals[i];
// get the closest multiple of ten to v
double mult = getMultipleOfTen(v);
// get the absolute distance of v from the multiple of ten
double delta = Math.abs(mult - v);
// get the bucket index based on the value of `mult`
int bIdx = (int)(mult / 10d);
// test the last known "smallest delta" for this bucket
if (buckets[bIdx][0] > delta)
{
// this is closer than the last known "smallest delta"
buckets[bIdx][0] = delta;
buckets[bIdx][1] = i;
buckets[bIdx][2] = v;
}
}
// print out the result
System.out.format(" 10x row value%n");
for (int i = 0; i < buckets.length; i++)
{
double[] bucket = buckets[i];
int multipleOfTen = i * 10;
double rowIndex = bucket[1];
double rowValue = bucket[2];
System.out.format(" %,2d %,4.0f %.4f%n",
multipleOfTen, rowIndex, rowValue);
}
}
public static double getMultipleOfTen(double v)
{
return 10d * Math.round(v / 10d);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다