java中如何从二维数组中减去整数值
我有一个二维数组java中如何从二维数组中减去整数值,java,arrays,subtraction,Java,Arrays,Subtraction,我有一个二维数组intarr[][]=newint[47][1]它将Resultset中的值保存为arr[i][j]=rs.getInt(2)。现在我想从我得到的所有行中减去10,20,30直到220,然后取接近值10,20到220的行。如何从二维数组中减去这些整数值 编辑1 我通过sql查询得到以下示例表 通过Resultset,我正在检索beam_current。正如您从表中看到的,有多行beam_current的值接近10,20,30,这正是我的问题。我想要的是一行对应于最接近10的每个
intarr[][]=newint[47][1]
它将Resultset中的值保存为arr[i][j]=rs.getInt(2)代码>。现在我想从我得到的所有行中减去10,20,30直到220,然后取接近值10,20到220的行。如何从二维数组中减去这些整数值
编辑1
我通过sql查询得到以下示例表
通过Resultset,我正在检索beam_current。正如您从表中看到的,有多行beam_current的值接近10,20,30,这正是我的问题。我想要的是一行对应于最接近10的每个beam_current值,20到220。我不知道如何通过sql查询来解决它,因此我是通过编码来解决的。下面是一个解决您问题的工作示例。
您首先必须读取数据库中的所有值,并将它们写入一个float[]
(因为您的值是float)。
然后,用您的值调用此方法
public static Map<Integer, Float> getClosestValues(float[] databaseValues) {
// We will store the results in this map
Map<Integer, Float> results = new LinkedHashMap<>();
// For each number in 10, 20, 30...210, we are going to look for the closest number
for (int number = 10; number <= 210; number += 10) {
float closest = getClosest(databaseValues, number)
// We have our closest value. Let's store it in the map.
results.put(number, closest);
}
// It's done!
float closestTo10 = results.get(10);
float closestTo20 = results.get(20);
float closestTo30 = results.get(30);
// ...
float closestTo210 = results.get(210);
return results;
}
private static float getClosest(float[] databaseValues, int number) {
float closest = databaseValues[0]; // Init the max with the first value
float closestDistance = Math.abs(number - closest);
for (float currentValue : databaseValues) {
float currentDistance = Math.abs(number - currentValue);
if (closestDistance > currentDistance) {
// We have a shortest distance! Let's update the max
closest = currentValue;
closestDistance = currentDistance;
}
}
return closest;
}
public静态映射getClosestValues(float[]数据库值){
//我们将结果存储在此地图中
映射结果=新建LinkedHashMap();
//对于10、20、30…210中的每个数字,我们将查找最接近的数字
用于(整数=10;数字当前距离){
//我们有最短的距离!让我们更新最大距离
最近值=当前值;
最近距离=当前距离;
}
}
返回最近的位置;
}
这绝对不是优化的,也不是精心设计的,但我详细介绍了每个步骤,以便您了解流程
- 您迭代表示参考号的
number
- 对于每一个,您都可以从数据库值中获得最接近的值
要获取给定数字的最接近值,请执行以下操作:
- Init与第一个值最接近
- 对于每个新值,如果其到数字的距离比我们拥有的距离短,那么它现在是最短的
- 当您迭代所有值时,您的值最接近李>
这是您一直在寻找的吗?下面是一个解决您问题的工作示例。
您首先必须读取数据库中的所有值,并将它们写入一个float[]
(因为您的值是float)。
然后,用您的值调用此方法
public static Map<Integer, Float> getClosestValues(float[] databaseValues) {
// We will store the results in this map
Map<Integer, Float> results = new LinkedHashMap<>();
// For each number in 10, 20, 30...210, we are going to look for the closest number
for (int number = 10; number <= 210; number += 10) {
float closest = getClosest(databaseValues, number)
// We have our closest value. Let's store it in the map.
results.put(number, closest);
}
// It's done!
float closestTo10 = results.get(10);
float closestTo20 = results.get(20);
float closestTo30 = results.get(30);
// ...
float closestTo210 = results.get(210);
return results;
}
private static float getClosest(float[] databaseValues, int number) {
float closest = databaseValues[0]; // Init the max with the first value
float closestDistance = Math.abs(number - closest);
for (float currentValue : databaseValues) {
float currentDistance = Math.abs(number - currentValue);
if (closestDistance > currentDistance) {
// We have a shortest distance! Let's update the max
closest = currentValue;
closestDistance = currentDistance;
}
}
return closest;
}
public静态映射getClosestValues(float[]数据库值){
//我们将结果存储在此地图中
映射结果=新建LinkedHashMap();
//对于10、20、30…210中的每个数字,我们将查找最接近的数字
用于(整数=10;数字当前距离){
//我们有最短的距离!让我们更新最大距离
最近值=当前值;
最近距离=当前距离;
}
}
返回最近的位置;
}
这绝对不是优化的,也不是精心设计的,但我详细介绍了每个步骤,以便您了解流程
- 您迭代表示参考号的
number
- 对于每一个,您都可以从数据库值中获得最接近的值
要获取给定数字的最接近值,请执行以下操作:
- Init与第一个值最接近
- 对于每个新值,如果其到数字的距离比我们拥有的距离短,那么它现在是最短的
- 当您迭代所有值时,您的值最接近李>
这是您一直在寻找的吗?下面是一个解决您问题的工作示例。
您首先必须读取数据库中的所有值,并将它们写入一个float[]
(因为您的值是float)。
然后,用您的值调用此方法
public static Map<Integer, Float> getClosestValues(float[] databaseValues) {
// We will store the results in this map
Map<Integer, Float> results = new LinkedHashMap<>();
// For each number in 10, 20, 30...210, we are going to look for the closest number
for (int number = 10; number <= 210; number += 10) {
float closest = getClosest(databaseValues, number)
// We have our closest value. Let's store it in the map.
results.put(number, closest);
}
// It's done!
float closestTo10 = results.get(10);
float closestTo20 = results.get(20);
float closestTo30 = results.get(30);
// ...
float closestTo210 = results.get(210);
return results;
}
private static float getClosest(float[] databaseValues, int number) {
float closest = databaseValues[0]; // Init the max with the first value
float closestDistance = Math.abs(number - closest);
for (float currentValue : databaseValues) {
float currentDistance = Math.abs(number - currentValue);
if (closestDistance > currentDistance) {
// We have a shortest distance! Let's update the max
closest = currentValue;
closestDistance = currentDistance;
}
}
return closest;
}
public静态映射getClosestValues(float[]数据库值){
//我们将结果存储在此地图中
映射结果=新建LinkedHashMap();
//对于10、20、30…210中的每个数字,我们将查找最接近的数字
用于(整数=10;数字当前距离){
//我们有最短的距离!让我们更新最大距离
最近值=当前值;
最近距离=当前距离;
}
}
返回最近的位置;
}
这绝对不是优化的,也不是精心设计的,但我详细介绍了每个步骤,以便您了解流程
- 您迭代表示参考号的
number
- 对于每一个,您都可以从数据库值中获得最接近的值
要获取给定数字的最接近值,请执行以下操作:
- Init与第一个值最接近
- 对于每个新值,如果其到数字的距离比我们拥有的距离短,那么它现在是最短的
- 当您迭代所有值时,您的值最接近李>
这是您一直在寻找的吗?下面是一个解决您问题的工作示例。
您首先必须读取数据库中的所有值,并将它们写入一个float[]
(因为您的值是float)。
然后,用您的值调用此方法
public static Map<Integer, Float> getClosestValues(float[] databaseValues) {
// We will store the results in this map
Map<Integer, Float> results = new LinkedHashMap<>();
// For each number in 10, 20, 30...210, we are going to look for the closest number
for (int number = 10; number <= 210; number += 10) {
float closest = getClosest(databaseValues, number)
// We have our closest value. Let's store it in the map.
results.put(number, closest);
}
// It's done!
float closestTo10 = results.get(10);
float closestTo20 = results.get(20);
float closestTo30 = results.get(30);
// ...
float closestTo210 = results.get(210);
return results;
}
private static float getClosest(float[] databaseValues, int number) {
float closest = databaseValues[0]; // Init the max with the first value
float closestDistance = Math.abs(number - closest);
for (float currentValue : databaseValues) {
float currentDistance = Math.abs(number - currentValue);
if (closestDistance > currentDistance) {
// We have a shortest distance! Let's update the max
closest = currentValue;
closestDistance = currentDistance;
}
}
return closest;
}
public静态映射getClosestValues(float[]数据库值){
//我们将结果存储在此地图中
映射结果=新建LinkedHashMap();
//对于10、20、30…210中的每个数字,我们将查找最接近的数字
用于(整数=10;数字当前距离){
//我们有最短的距离!让我们更新最大距离
最近值=当前值;
最近距离=当前距离;
}
}
返回最近的位置;
}
这绝对不是优化的,也不是精心设计的,但我详细介绍了每个步骤,以便您了解流程
- 您在
number