简体   繁体   English

在 Java 中删除数组的最高和最低

[英]Drop highest and lowest of an array in Java

right now I have a project dealing with fractions.现在我有一个处理分数的项目。 The fractions need to be sorted and the highest and lowest are dropped, and then the middle fractions are added together.需要对分数进行排序,丢弃最高和最低的分数,然后将中间的分数相加。 I have created the array, and done the sort.我已经创建了数组,并完成了排序。 I am stuck as to how to drop the highest and lowest.我不知道如何降低最高和最低。 Any help is appreciated, thanks!任何帮助表示赞赏,谢谢!

public class Tester {

public static void main(String[] args) {
    double[] fractions = {(6/7),(2/4),(3/4),(3/18),(1/8),(10/20),(2/6)};
}
public static void selectionSort (int... arr)
{
    int i = 0, j = 0, smallest = 0;
    int temp = 0;

    for (i = 0;i<arr.length - 1;i++)
    {
        smallest = i;
        for (j = 1; j<arr.length - 1; j++)
        {
            if (arr[j]<arr[smallest])
                smallest = j;
        }
        temp = arr[smallest];
        arr[smallest] = arr[i];
        arr[i] = temp;

    }

    //Drop highest and lowest here
}
}

First off you have to add decimal points to either the denominator or numerator in your array initialization to get an array that is not full of 0 's (That way you are not doing integer math). 首先,您必须在数组初始化的分母或分子中添加小数点,以获取未满0的数组(这样您就不会进行整数数学运算)。 Or simply add a d at the end of your integer math ( 6/7d ). 或者只是在整数数学运算( 6/7d )的末尾添加d

Next you can sort using Arrays.sort (or continue using your custom sort method) and you can get a sub array using Arrays.copyOfRange . 接下来,您可以使用Arrays.sort进行Arrays.sort (或继续使用自定义排序方法),并且可以使用Arrays.copyOfRange获得子数组。 This is very handy since you can give it a starting index and an ending index. 这非常方便,因为您可以为其指定开始索引和结束索引。 Something like this should do the trick: 这样的事情应该可以解决问题:

public static void main(String[] args) {
    double[] fractions = {(6./7),(2./4),(3./4),(3./18),(1./8),(10./20),(2./6)}; //Add decimal points
    Arrays.sort(fractions); //Sort your array
    double[] removeLowestAndHighest = Arrays.copyOfRange(fractions, 1, fractions.length-1); //returns new array with first and last element removed.    
}

我建议使用List,然后您可以简单地使用.remove(0)、. remove(size-1)

// Create a new array that is 2 smaller than the orriginal.
int[] newArr = new int[arr.length-2];  
// Iterate through the old array, skipping the first and last elements.
for(int i = 0; i < newArr.length; i++)
{
    newArr[i] = arr[i+1];
}

You could convert the array to a list to remove the min/max and then convert back to an array if necessary 您可以将数组转换为列表以删除最小值/最大值,然后根据需要转换回数组

You could use Arrays.asList(array) for the conversion 您可以使用Arrays.asList(array)进行转换

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

Collections has a min and a max method you can use on the list to determine what you need to remove 您可以在列表中使用“收藏夹”的最小和最大方法来确定需要删除的内容

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM