Java 如何删除值,确定剩余值的最小值

Java 如何删除值,确定剩余值的最小值,java,for-loop,arraylist,netbeans,Java,For Loop,Arraylist,Netbeans,我需要在删除第一个值后确定最小值 例如,这些是数字0.570809010 我需要删除0.5,确定剩余数字中的最小值。calweightAvg是我关注的焦点 最终输出应为“当使用数据0.5 70 80 90 10时,数字的加权平均值为40,其中0.5为权重,在删除其余值中的最低值后计算平均值。” 编辑:一切似乎都在运作,除了在最后的输出。“使用数据70.0、80.0、90.0、10.0时,数字的加权平均值为40.0,其中70.0(应为0.5)是权重,平均值是在去掉其余值中的最低值后计算的。” 因此

我需要在删除第一个值后确定最小值

例如,这些是数字0.570809010

我需要删除0.5,确定剩余数字中的最小值。calweightAvg是我关注的焦点

最终输出应为“当使用数据0.5 70 80 90 10时,数字的加权平均值为40,其中0.5为权重,在删除其余值中的最低值后计算平均值。”

编辑:一切似乎都在运作,除了在最后的输出。“使用数据70.0、80.0、90.0、10.0时,数字的加权平均值为40.0,其中70.0(应为0.5)是权重,平均值是在去掉其余值中的最低值后计算的。”

因此,数学是正确的,而输出是错误的

编辑:使用类
静态双权重=0.5时
以建立权重,如果用户要更改输入文件中的值,则该操作无效。我怎样才能换班

/*
* 
*/
package calcweightedavg;
import java.util.Scanner;
import java.util.ArrayList;

import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;


public class CalcWeightedAvg {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {

    //System.out.println(System.getProperty("user.dir"));
    ArrayList<Double> inputValues = getData(); // User entered integers.
    double weightedAvg = calcWeightedAvg(inputValues); // User entered weight.
    printResults(inputValues, weightedAvg); //Weighted average of integers. 

}

public class CalcWeightedAvg {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {

    //System.out.println(System.getProperty("user.dir"));
    ArrayList<Double> inputValues = getData(); // User entered integers.
    double weightedAvg = calcWeightedAvg(inputValues); // User entered weight.
    printResults(inputValues, weightedAvg); //Weighted average of integers. 

}

public static ArrayList<Double> getData() throws FileNotFoundException {

    // Get input file name.
    Scanner console = new Scanner(System.in);
    System.out.print("Input File: ");
    String inputFileName = console.next();
    File inputFile = new File(inputFileName);

    // 
    Scanner in = new Scanner(inputFile);
    String inputString = in.nextLine();

    // 
    String[] strArray = inputString.split("\\s+"); //LEFT OFF HERE

    // Create arraylist with integers.
    ArrayList<Double> doubleArrayList = new ArrayList<>();
    for (String strElement : strArray) {
        doubleArrayList.add(Double.parseDouble(strElement));
    }

    in.close();

    return doubleArrayList;  
}


public static double calcWeightedAvg(ArrayList<Double> inputValues){
   //Get and remove weight.
   Double weight = inputValues.get(0);
   inputValues.remove(0);

   //Sum and find min.
   double min = Double.MAX_VALUE;
   double sum = 0;
   for (Double d : inputValues) {
       if (d < min) min = d;
       sum += d;
   }

   // Calculate weighted average.
   return (sum-min)/(inputValues.size()-1) * weight;


}

public static void printResults(ArrayList<Double> inputValues, double weightedAvg) throws IOException {
    Scanner console = new Scanner(System.in);
    System.out.print("Output File: ");
    String outputFileName = console.next();
    PrintWriter out = new PrintWriter(outputFileName);

    System.out.println("Your output is in the file " + outputFileName);

    out.print("The weighted average of the numbers is " + weightedAvg + ", ");
    out.print("when using the data ");
    for (int i=0; i<inputValues.size(); i++) {
        out.print(inputValues.get(i) + ", ");
    }
    out.print("\n where " + inputValues.get(0) + " is the weight, ");
    out.print("and the average is computed after dropping the lowest of the rest of the values.\n");

    out.close();
}
/*
* 
*/
包装CalcWg;
导入java.util.Scanner;
导入java.util.ArrayList;
导入java.io.File;
导入java.io.PrintWriter;
导入java.io.FileNotFoundException;
导入java.io.IOException;
公共类CalcWg{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发IOException{
//System.out.println(System.getProperty(“user.dir”);
ArrayList inputValues=getData();//用户输入的整数。
double weightedAvg=CalcWg(inputValues);//用户输入的权重。
printResults(inputValues,weightedAvg);//整数的加权平均值。
}
公共类CalcWg{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发IOException{
//System.out.println(System.getProperty(“user.dir”);
ArrayList inputValues=getData();//用户输入的整数。
double weightedAvg=CalcWg(inputValues);//用户输入的权重。
printResults(inputValues,weightedAvg);//整数的加权平均值。
}
公共静态ArrayList getData()引发FileNotFoundException{
//获取输入文件名。
扫描仪控制台=新扫描仪(System.in);
System.out.print(“输入文件:”);
字符串inputFileName=console.next();
文件inputFile=新文件(inputFileName);
// 
扫描仪输入=新扫描仪(输入文件);
String inputString=in.nextLine();
// 
String[]strArray=inputString.split(“\\s+”);//此处省略
//使用整数创建arraylist。
ArrayList doubleArrayList=新的ArrayList();
用于(字符串元素:strArray){
doubleArrayList.add(Double.parseDouble(strElement));
}
in.close();
返回doubleArrayList;
}
公共静态双CalcWg(ArrayList inputValues){
//取出并卸下重物。
双重权重=输入值。获取(0);
输入值。删除(0);
//求和求最小值。
双最小值=双最大值;
双和=0;
for(双d:输入值){
如果(d对于(int i=0;i如果要从
ArrayList
中删除第一个元素并计算剩余元素的最小值,则应执行以下操作:

if(inputValues.size() <= 1) //no point in calculation of one element
   return;

inputValues.remove(0);
double min = inputValues.get(0); 
for (int i = 1; i < inputValues.size(); i++) {
     if (inputValues.get(i) < min)
           min = inputValues.get(i);
} 

if(inputValues.size()既然您使用的是ArrayList,这应该是小菜一碟

要从ArrayList中删除一个值,只需找到该值的索引并调用

myList.remove(index);
如果0.5是列表中的第一个元素,请使用

inputValues.remove(0);
如果要在双精度数组列表中查找最小值,只需使用此算法查找最小值及其索引:

double minVal = Double.MAX_VALUE;
int minIndex = -1;

for(int i = 0; i < myList.size(); i++) {

    if(myList.get(i) < minVal) {
        minVal = myList.get(i);
        minIndex = i;
    }
}
double minVal=double.MAX_值;
int minIndex=-1;
对于(int i=0;i

希望这能有所帮助!

我对您的目标有点不清楚。如果您需要频繁调用以检查最小值,最小堆将是一个非常好的选择

最小堆的属性是提供对最小值的恒定时间访问。此[]使用ArrayList。因此,您可以使用
add()
方法和
minValue()添加到ArrayList
提供对列表最小值的恒定时间访问,因为它确保最小值始终位于索引0处。当删除最小值或添加新值(称为heapify)时,列表将相应地修改

我不会在这里添加任何代码,因为链接应该会让这部分变得清晰。如果你想得到一些澄清,我将非常乐意为你提供帮助

编辑

public class HelloWorld {
    private static ArrayList<Double> values;
    private static Double sum = 0.0D;

    /**
     * Identifies the minimum value stored in the heap
     *   @return the minimum value
     */
    public static Double minValue() {
        if (values.size() == 0) {
            throw new NoSuchElementException();
        }
        return values.get(0);
    }

    /**
     * Adds a new value to the heap.
     *   @param newValue the value to be added
     */
    public static void add(Double newValue) {
        values.add(newValue);
        int pos = values.size()-1;

        while (pos > 0) {
            if (newValue.compareTo(values.get((pos-1)/2)) < 0) {
                values.set(pos, values.get((pos-1)/2));
                pos = (pos-1)/2;
            }
            else {
                break;
            }
        }
        values.set(pos, newValue);

        // update global sum
        sum += newValue;
    }

    /**
     * Removes the minimum value from the heap.
     */
    public static void remove() {
       Double newValue = values.remove(values.size()-1);
       int pos = 0;

       if (values.size() > 0) {
           while (2*pos+1 < values.size()) {
               int minChild = 2*pos+1;
               if (2*pos+2 < values.size() &&
                     values.get(2*pos+2).compareTo(values.get(2*pos+1)) < 0) {
                   minChild = 2*pos+2;
               }

               if (newValue.compareTo(values.get(minChild)) > 0) {
                   values.set(pos, values.get(minChild));
                   pos = minChild;
               }
               else {
                   break;
               }
           }
           values.set(pos, newValue);

       }      
       // update global sum
       sum -= newValue;
    }

    /**
     * NEEDS EDIT Computes the average of the list, leaving out the minimum value.
     *   @param newValue the value to be added
     */
    public static double calcWeightedAvg() {
        double minValue = minValue();
        // the running total of the sum took this into account
        // so, we have to remove this from the sum to get the effective sum
        double effectiveSum = (sum - minValue);

        return effectiveSum * minValue;
    }

    public static void main(String []args) {
        values = new ArrayList<Double>();

        // add values to the arraylist -> order is intentionally ruined
        double[] arr = new double[]{10,70,90,80,0.5};
        for(double val: arr)
            add(val);

        System.out.println("Present minimum in the list: " + minValue());    // 0.5
        System.out.println("CalcWeightedAvg: " + calcWeightedAvg());    // 125.0
    }
}
公共类HelloWorld{
私有静态ArrayList值;
专用静态双和=0.0D;
/**
*标识存储在堆中的最小值
*@返回最小值
*/
公共静态双最小值(){
if(values.size()==0){
抛出新的NoTouchElementException();
}
返回值。get(0);
}
/**
*将新值添加到堆中。
*@param newValue要添加的值
*/
公共静态void add(双新值){
添加(newValue);
int pos=values.size()-1;
而(位置>0){
if(newValue.compareTo(values.get((pos-1)/2))<0{
值。设置(位置,值。获取((位置-
if(inputValues.size() <= 1){
       inputValues.remove(0);
    }
ArrayList<Double> list = new ArrayList();
        // get and remove weight
        Double weight = list.get(0);
        list.remove(0);
        // sum and find min
        double min=Double.MAX_VALUE;
        double sum=0;
        for (Double d : list) {
            if (d<min) min = d;
            sum+=d;
        }
        // subtract min value from sum
        sum-=min;
        // apply weight
        sum*=weight;
        // calc weighted avg
        double avg = sum/list.size()-1;
        // viola!
public static double calcWeightedAvg(ArrayList<Double> inputValues){
   //Get and remove weight.
   Double weight = inputValues.get(0);
   inputValues.remove(0);

   //Sum and find min.
   double min = Double.MAX_VALUE;
   double sum = 0;
   for (Double d : inputValues) {
       if (d < min) min = d;
       sum += d;
   }
   // Calculate weighted average. 
   double returnVal = (sum-min)/(inputValues.size()-1) * weight;
   // add weight back to list
   inputValues.add(0,weight);

   return returnVal;
}
public class CalcWeightedAvg { 

static double weight=0;
//...
}
public static double calcWeightedAvg(ArrayList<Double> inputValues){
   //Get and remove weight.
   weight = inputValues.get(0); // changed to class variable
//...
}
out.print("\n where " + weight + " is the weight, ");