首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java对象[]排序

Java对象[]排序
EN

Stack Overflow用户
提问于 2013-12-02 02:36:28
回答 4查看 136关注 0票数 1

每次添加对象时,我都试图对创建的对象数组进行排序。我编写了一个compareTo方法,它正在打印每一行,但当我尝试对其排序时会抛出一个异常。我正在初始化一个由99个元素组成的Element[] (MaxLen),然后使用topIndex作为计数器来查找“真实”长度。扫描器用于获取用户输入以创建元素。编辑--我添加了完整的代码。

代码语言:javascript
运行
复制
import java.util.Arrays;
import java.util.Scanner;



public class mainmenu {
static Element[] data = new Element[100];
static int maxLen= 99; 
static int topIndex= -1;
@SuppressWarnings("rawtypes")


public static void main(String[] args){


    menu();


}


public static void menu(){
    boolean leave = true;
    Scanner in = new Scanner(System.in);
    while(leave){
        //loop. unless value is 1-7, keeps recycling    
        System.out.println("Please select a menu option: ");
        System.out.println("'1'- enque ");
        System.out.println("'2'- deque");
        System.out.println("'3'- peek");
        System.out.println("'4'- display");
        System.out.println("'5' empty queue");
        System.out.println("'6'- check if the queue is empty");
        System.out.println("'7' to exit the program");
        String select = in.next();
        if ((select.equals("1") || select.equals("2")|| select.equals("3")|| select.equals("4")|| 
                select.equals("5")|| select.equals("6")|| select.equals("7")))
            leave = callMethods(select );
        else{
            System.out.println("Please enter a valid menu option.");
        }
    }
}

public static  boolean callMethods(String select )
{
    boolean leave= true;
    int sel = Integer.parseInt(select);

    switch(sel){
    case 1:

        enqueue();

        break;
    case 2:
        dequeue();
        break;
    case 3:
        peek();

        break;
    case 4:
        display();

        break;
    case 5:
        empty();

        break;
    case 6:
        if( isEmpty()){
            System.out.println("The structure is empty");
        }
        else{
            System.out.println("The structure is not empty.");
        }
        break;
    case 7:
        System.out.println("Bye!");
        leave = false;
    }
    return leave;
}

public static void enqueue(){
    boolean isInt = false;
    String st = null;
    int index = 0;
    Scanner in = new Scanner(System.in);

    while(!isInt){  //loop continues until input is an integer type
        System.out.println("Please enter a priority level for this element");
        st = in.next();
        if (isInteger(st) == true){// calls method to check if value is integer
            index = Integer.parseInt(st);//parses the string into a integer
            isInt = true;
        }
        else //if value isnt integer, try again
            System.out.println("Invalid Input.");
    }
    System.out.println("Please enter the string of information.");
    String info = in.next();

    Element e = new Element(info, index);

    if (topIndex == maxLen){
        System.out.println("Data Structure is full.");
    }
    else if (isEmpty()){
        data[0] = e;
        topIndex++;
    }
    else {
        topIndex++;
        data[topIndex]=e;
        System.out.println("Added "+ e.getPriority() + " at "+ e.getInfo());
        System.out.println(topIndex);

        Arrays.sort(data);

    }

}

private static boolean isInteger(String s) {
    //checks to see if string can be parsed as an integer.


    try{
        Integer.parseInt(s);
        return true;
    }
    catch( Exception e ){
        return false;
    }


}
public static void dequeue(){
    if(isEmpty()){
        System.out.println("The structure is empty.");
    }
    else{
        Element e = data[topIndex];
        System.out.println("Removing Element: " + e.getInfo()+ " Priority Level: " + e.getPriority());
        --topIndex;
    }
}

public static void peek(){
    if (isEmpty()){
        System.out.println("The structure is empty.");

    }
    else {
        Element e = (data[0]);
        System.out.println("Element: " +  e.getInfo()+ " Priority Level: " + e.getPriority());
    }
}

public static void display(){
    System.out.println("topIndex " + topIndex);
    if (topIndex==-1){
        System.out.println("The structure is empty.");

    }
    else {
        for(int i = 0; i <= topIndex; i++){
            System.out.println("Index: " +i);
            Element e = data[i];
            System.out.println("Element: " + e.getInfo()+ " Priority Level: " + e.getPriority());

        }
    }
}

public static void empty(){
    System.out.println("Erasing data.");
    topIndex=-1;
}

public static boolean isEmpty(){
    return (topIndex==-1);
}

}

元素类:

代码语言:javascript
运行
复制
public class Element implements Comparable<Element>  {

private String info;
private int index;

public Element ( String st, int ind) {
    super();
    this.info = st;
    this.index= ind;

}


public String getInfo() {
    return info;
}
public void setInfo(String st) {
    this.info = st;
}

public int getPriority(){
    return index;
}
public void setPriority(int pr){
this.index = pr;
}


public int compareTo(Element e) {
    System.out.println(e.index+ ""+ e.info);
//  if (!(e instanceof Element))
//      throw new ClassCastException("An Element object expected.");
    int ePriority = e.getPriority();  
    System.out.println(ePriority);
    System.out.println(this.index);
    int balls= this.index - ePriority;    
    System.out.println(balls);
    return balls;
}

}

EN

Stack Overflow用户

发布于 2013-12-02 02:43:08

在进行比较时,您正在决定一个对象是否以某种任意条件超过另一个对象。

我注意到,您不检查与之比较的对象是否为null --您应该这样做。

代码语言:javascript
运行
复制
public int compareTo(Element e) {
    if(e == null) {
        return this.index;
    } else {
        // rest of your logic goes here
    }
}
票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20319968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档