Java 根据arrayList中的特定值对其进行分组
我的代码如下:Java 根据arrayList中的特定值对其进行分组,java,arrays,sorting,arraylist,Java,Arrays,Sorting,Arraylist,我的代码如下: import java.util.ArrayList; 导入java.util.array; 导入java.util.List 公共类排序{ public static void main(String[] args) { // TODO Auto-generated method stub List<String> theMenu = new ArrayList<String>(); String[] Array1 = {
import java.util.ArrayList;
导入java.util.array;
导入java.util.List
公共类排序{
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);
}
在ArrayList中。无论我们在ArrayList中存储了多少项,这都应该有效。我的代码生成以下输出:
[M1, Wine, 2.50, M2, Soft drink, 1.50, D1, Fish, 7.95, D2, Veg chili, 6.70]
注意:忘记数组中的新行,我只需要整理索引。有人能帮我吗?首先,你有一个实体-产品,它有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 Compariable来方便写排序。当我们实现这个interphace时,我们必须重写
public int compareTo(Object o)
{
MyObject receivedObject = (MyObject) o;
return this.id.compareTo(receivedObject.getId());
}
此方法比较两个对象并说明哪个对象更大。在您的情况下,我们只需要通过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='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}
他说他不能让一个类包含这些信息。问题陈述太奇怪了,要么是家庭作业,要么是面试问题。不过,选择你最喜欢的排序算法并把arraylist分成三组应该不会那么难。
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}