如何使用java将对象插入到正确的位置?

如何使用java将对象插入到正确的位置?,java,sorting,insertion,Java,Sorting,Insertion,我的程序有一个对象目录,每个对象都有标题、项目编号、歌曲艺术家等。我需要使用我已经编写的代码并更改addItem方法,这样它就不会将项目放在数组的末尾,而是将其放在数组中的正确位置,以便保持排序。我试图通过比较项目类型,首先按字母顺序(书籍、电影或音乐)进行比较,如果它们相同,我将按项目编号进行比较。插入对象后,我不会对整个数组进行排序,因此我知道我只需要一个循环,而不是两个嵌套的循环。唯一的问题是,我看到的唯一方法是,如果数组本身是方法的参数之一,那么方法头应该是这样的 public sta

我的程序有一个对象目录,每个对象都有标题、项目编号、歌曲艺术家等。我需要使用我已经编写的代码并更改addItem方法,这样它就不会将项目放在数组的末尾,而是将其放在数组中的正确位置,以便保持排序。我试图通过比较项目类型,首先按字母顺序(书籍、电影或音乐)进行比较,如果它们相同,我将按项目编号进行比较。插入对象后,我不会对整个数组进行排序,因此我知道我只需要一个循环,而不是两个嵌套的循环。唯一的问题是,我看到的唯一方法是,如果数组本身是方法的参数之一,那么方法头应该是这样的

 public static void insertionSort (Comparable[] list)
但是,我需要在不更改任何方法头的情况下执行此操作,因为如果我这样做,则必须在主方法中更改它们。我的add方法现在看起来像这样。唯一的参数是要添加到数组中的新对象

  public void addItem(Item theItem)
    {

        items[size] = theItem;
        size++;


    }



//This Is a class for a general Item 
public class Item implements Comparable<Item>
{
    // instance variables - replace the example below with your own
    private int itemnum;
    private String title;
    private double price;

    /**
     * Constructor for objects of class Item
     */
    public Item(int id, String t, double p)
    {
        // initialise instance variables
        itemnum = id;
        title = t;
        price = p;
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public int getItemNumber()
    {
        // put your code here
        return itemnum;
    }

    public String getItemType()
    {
        return "Item";
    }

    public String getItemTitle()
    {
      return title;  
    }

    public String toString()
    {
        String line1, line2, line3, line4, out;
        String itemtype = this.getItemType();
        line1 = String.format("Item number: %d%n", itemnum);
        line2 = String.format("Item type: %s%n", itemtype);
        line3 = String.format("Item title: %s%n", title);
        line4 = String.format("Item price: %.2f%n", price);
        out = line1 + line2 + line3 + line4 + "\n";
        return out;
    } 
    public int compareTo(Item other){
        //items are equal
        if(this.getItemType().equals(other.getItemType())){
           return this.itemnum - other.itemnum;
        }
        //item types are not equal
        else
        return this.getItemType().compareTo(other.getItemType());


    }
}
公共作废附加项(项)
{
项目[规模]=利润项目;
大小++;
}
//这是一个用于一般项目的类
公共类项实现了可比性
{
//实例变量-将下面的示例替换为您自己的
私有int itemnum;
私有字符串标题;
私人双价;
/**
*类项的对象的构造函数
*/
公共项(整数id、字符串t、双p)
{
//初始化实例变量
itemnum=id;
title=t;
价格=p;
}
/**
*方法示例-将此注释替换为您自己的注释
*
*@param y方法的示例参数
*@返回x和y的总和
*/
public int getItemNumber()
{
//把你的代码放在这里
返回itemnum;
}
公共字符串getItemType()
{
返回“项目”;
}
公共字符串getItemTitle()
{
返回标题;
}
公共字符串toString()
{
字符串line1、line2、line3、line4、out;
String itemtype=this.getItemType();
line1=String.format(“项目编号:%d%n”,itemnum);
line2=String.format(“项目类型:%s%n”,项目类型);
line3=String.format(“项目标题:%s%n”,标题);
第4行=String.format(“项目价格:%.2f%n”,价格);
输出=第1行+第2行+第3行+第4行+“\n”;
返回;
} 
公共整数比较(项目其他){
//项目是平等的
if(this.getItemType().equals(other.getItemType())){
返回this.itemnum-other.itemnum;
}
//项目类型不相等
其他的
返回此.getItemType().compareTo(其他.getItemType());
}
}

我如何使用一个对象本身作为参数将一个对象插入一个数组到正确的位置?

是否考虑使用LINKEDLIST来处理该情况?第一,它使数据保持有序(默认情况下为插入顺序),第二,它提供在特定索引处添加/删除对象的选项。参见下面的示例(摘自Google上的快速搜索)

import java.util.*;
公共类JavaExample{
公共静态void main(字符串参数[]){
LinkedList=新建LinkedList();
//向链接列表中添加元素
列表。添加(“史蒂夫”);
列表。添加(“卡尔”);
列表。添加(“Raj”);
//将元素添加到第一个位置
列表。先添加(“内甘”);
//将元素添加到最后一个位置
列表。addLast(“瑞克”);
//将元素添加到第三个位置
列表。添加(2,“格伦”);
//迭代链接列表
迭代器迭代器=list.Iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
} 
} 

Comparable
是一种无法直接使用的类型:除了原始类型之外,
newcomparable[]{0,“}
是合法的,但对不同类型的对象进行排序没有意义。如果要传递一个相互比较的对象数组,请将该方法声明为,例如,
publicstatic。实际上,对象的类型并不不同。它们都是道具,我还有其他的课程叫音乐,电影,扩展项的书我只想把它们按字母顺序按类型排列,按数字顺序按项目编号排列我最初写的是Arraylist程序,但最后的任务是修改它,使它使用数组而不是Arraylist,所以很遗憾,我只能使用数组。实际上,我唯一应该更改的是addItem方法中的代码。
import java.util.*;
public class JavaExample{
   public static void main(String args[]){

     LinkedList<String> list=new LinkedList<String>();

     //Adding elements to the Linked list
     list.add("Steve");
     list.add("Carl");
     list.add("Raj");

     //Adding an element to the first position
     list.addFirst("Negan");

     //Adding an element to the last position
     list.addLast("Rick");

     //Adding an element to the 3rd position
     list.add(2, "Glenn");

     //Iterating LinkedList
     Iterator<String> iterator=list.iterator();
     while(iterator.hasNext()){
       System.out.println(iterator.next());
     }
   } 
}