使用分组

时间:2016-11-28 16:39:13

标签: java arrays sorting arraylist

我的代码如下:

import java.util.ArrayList;

import java.util.Arrays; import java.util.List;

public class Sorting {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<String> theMenu = new ArrayList<String>();
    String[] Array1 = {
             "M1", "Wine",        "2.50",
              "M2", "Soft drink",  "1.50",
             "D1", "Fish",        "7.95",
             "D2", "Veg chili",   "6.70"
    };
    theMenu.addAll(Arrays.asList(Array1));

    String[] temp1 = new String[3];
    String[] temp2 = new String[3];

    for (int i = 0; i < theMenu.size(); i+=3) {
        for (int j = i + 3; j < theMenu.size(); j+=3) {
        if (i < theMenu.size() - 3) {
        if (theMenu.get(i).compareTo(theMenu.get(i + 3)) > 0) {


            temp1[0] = theMenu.get(i);
            temp1[1] = theMenu.get(i + 1);
            temp1[2] = theMenu.get(i + 2);

            temp2[0] = theMenu.get(j);
            temp2[1] = theMenu.get(j+1);
            temp2[2] = theMenu.get(j+2);

            theMenu.remove(j  + 2);
            theMenu.remove(j + 1);
            theMenu.remove(j);
            theMenu.remove(i + 2);
            theMenu.remove(i + 1);
            theMenu.remove(i);

            theMenu.add(i, temp2[0]);
            theMenu.add(i + 1, temp2[1]);
            theMenu.add(i + 2, temp2[2]);

            theMenu.add(j, temp1[0]);
            theMenu.add(j + 1, temp1[1]);
            theMenu.add(j + 2, temp1[2]);


        }
        }

        }

    }

    System.out.println(theMenu);

}

}

我想按D1,D2,M1,M2,M3的顺序对ArrayList进行排序,同时保持其各自的项目和价格与ID。我不允许更改存储方法,即使另一类具有自己的ID和名称和价格的项目。如何重新排列它以使其形式为:

{"D1" , "Fish", "7.95"
 "D2" , "Veg chili", "6.70",
 "M1" , "Wine", "2.50",
 "M2", "Soft drink", "1.50"
 }

在ArrayList中。无论我们在arrayList中存储了多少项,这都应该有效。我的代码产生以下输出:

[M1, Wine, 2.50, M2, Soft drink, 1.50, D1, Fish, 7.95, D2, Veg chili, 6.70]

注意:忘记数组中的新行,我只需要整理索引。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:-1)

首先,您有一个实体 - 产品,其中包含ID,名称和价格。 始终为应用程序中的每个实体创建新类。 例如:

public class MyObject implements Comparable
{
  private String id;
  private String name;
  private double price;

  public MyObject(String id, String name, double price)
  {
    this.id = id;
    this.name = name;
    this.price = price;
  }

  public String getId()
  {
    return id;
  }

  @Override
  public int compareTo(Object o)
  {
    MyObject receivedObject = (MyObject) o;
    return this.id.compareTo(receivedObject.getId());
  }

  @Override
  public String toString()
  {
    return "MyObject{" +
        "id='" + id + '\'' +
        ", name='" + name + '\'' +
        ", price=" + price +
        '}';
  }
}

我使用“implements Comparable”进行简单的写入排序。当我们实现这个interphace时,我们必须覆盖

public int compareTo(Object o)
{
  MyObject receivedObject = (MyObject) o;
  return this.id.compareTo(receivedObject.getId());
}

此方法比较2个对象并说出哪个对象“更大”。在您的情况下,我们只需要通过ID进行比较。

现在您拥有能够相互比较的实体。检查一下:

public class Processing
{
  public static void main(String[] argc) {
    List<MyObject> list = new ArrayList<>();
    list.add(new MyObject("M1", "Wine", 2.50));
    list.add(new MyObject("M2", "Soft drink", 1.50));
    list.add(new MyObject("D1", "Fish", 7.95));
    list.add(new MyObject("D2", "Veg chili", 6.70));

    System.out.println(list);

    Collections.sort(list);
    System.out.println(list);
  }
}

首先输出:

MyObject{id='M1', name='Wine', price=2.5},
MyObject{id='M2', name='Soft drink', price=1.5},
MyObject{id='D1', name='Fish', price=7.95},
MyObject{id='D2', name='Veg chili', price=6.7}

第二次输出:

MyObject{id='D1', name='Fish', price=7.95},
MyObject{id='D2', name='Veg chili', price=6.7},
MyObject{id='M1', name='Wine', price=2.5},
MyObject{id='M2', name='Soft drink', price=1.5}