简体   繁体   English

如何从Java中的数据数组中删除最少的项目?

[英]How to remove minimum item from data array in Java?

I am working on a homework assignment in which I need to write a method that takes a data array and removes the minimum item from it. 我正在做一项家庭作业,在其中我需要编写一个采用数据数组并从中删除最少项目的方法。 I am able to find the minimum item, but I am having trouble removing it. 我能够找到最小的物品,但是我无法将其删除。 My code is below. 我的代码如下。

  public Comparable removeMin() {

    Iterator<T> it = iterator();
    T min = it.next();
    while (it.hasNext()) {
        T next = it.next();
        if (min.compareTo(next) > 0)
            min = next;
        it.remove();

    }
    System.out.println(min);
    return min;
}

I have the print statement there just to verify that it is in fact getting the minimum item. 我在那儿有打印语句,只是用来验证它实际上是否得到最少的物料。

The code in main looks like this. main中的代码如下所示。

public static void main(String[] args) {

  Bag<String> sbag = new Bag<String>();

    sbag.add("Noriko");
    sbag.add("Peter"); 
    sbag.add("Buddy");
    sbag.add("Mary");

    sbag.removeMin();

  }

When I run the sbag.removeMin(); 当我运行sbag.removeMin(); command, the console prints "Buddy" which tells me that it is choosing "Buddy" as the min item. 命令,控制台将显示“ Buddy”,告诉我它正在选择“ Buddy”作为最小项。 However, if I instead populate sbag with integers, it always prints the smallest integer, which indicates to me I have my code correct in that regard. 但是,如果我改为用整数填充sbag,它将始终打印最小的整数,这向我表明我在这方面的代码正确。

I have two questions. 我有两个问题。

1). 1)。 When passing a list of strings such as above, how does Java determine which is smallest? 当传递诸如上述的字符串列表时,Java如何确定最小的字符串?

2). 2)。 How can I fix my code so that it not only finds the minimum item, but also removes it? 如何修复我的代码,以便它不仅找到最小的项目,还删除它?

First, it compares String based on the integral value of the characters in the String (eg ASCII) (but it does that because String implements Comparable<String> ). 首先,它比较String基于所述字符的在积分值String (例如ASCII)(但它确实,由于String实现Comparable<String> )。 I think you meant to return the generic type T , and you should check for an empty Collection . 我认为您打算返回通用类型T ,并且应该检查一个空的Collection Finally, you need to iterate twice. 最后,您需要迭代两次。 Once to find the min the second to remove it. 一次找到min ,第二min将其删除。

public T removeMin() {
    Iterator<T> it = iterator();
    T min = (it.hasNext()) ? it.next() : null;
    while (it.hasNext()) {
        T next = it.next();
        if (min.compareTo(next) > 0) {
            min = next;
        }
    }
    it = iterator(); // <-- start again
    while (it.hasNext()) {
        T next = it.next();
        if (min.compareTo(next) == 0) {
            it.remove();
            break;
        }
    }
    System.out.println(min);
    return min;
}

It's using the ASCII code. 它使用ASCII码。 In numeric byte values, B is lower in value than anything else you've got there. 在数字字节值中,B的值比那里的其他值低。

Andto get rid of it, use array[n]=""; 要摆脱它,请使用array[n]="";

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

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