Java 有人能帮我用数组列表中的比较进行排序吗?
嗨,伙计们,我正在做一个库存计划,我对库存项目排序方法有问题。我需要使用compareto来解决它。 这是到目前为止我的代码。请滚动到底部,看看我在说什么Java 有人能帮我用数组列表中的比较进行排序吗?,java,sorting,arraylist,compareto,Java,Sorting,Arraylist,Compareto,嗨,伙计们,我正在做一个库存计划,我对库存项目排序方法有问题。我需要使用compareto来解决它。 这是到目前为止我的代码。请滚动到底部,看看我在说什么 public class Inventory { private ArrayList<StockItem> stock; public Inventory() { stock = new ArrayList<StockItem>(); } public void ad
public class Inventory {
private ArrayList<StockItem> stock;
public Inventory() {
stock = new ArrayList<StockItem>();
}
public void addStockItem(StockItem item) {
stock.add(item);
}
public int size() {
return stock.size();
}
public String toString() {
String result = "";
for(StockItem item: stock)
result+=item.toString()+"\n";
return result;
}
public boolean isValidIndex(int index) {
return index >=0 && index < stock.size();
}
/**
*
* @param index
* @return null if index is not valid, otherwise
* return item at that index
*/
public StockItem getItem(int index) {
if (index < 0 || index >= this.stock.size())// check if this index exists
return null; // removes the item the from stock and returns it
else
return this.stock.get(index);
}
/**
*
* @param index
* @return null if index is invalid, otherwise remove item at the given
* index and return the removed item.
*/
public StockItem remove(int index) {
if (index < 0 || index >= this.stock.size()) // check if this index exists
return null; // removes the item the from stock and returns it
else
return this.stock.remove(index);
}
/**
* sort, using {@link StockItem#compareTo(StockItem)}
* cannot use built-in sort method from java
*/
public void sort() {
}
公共类目录{
私人ArrayList股票;
公共库存(){
stock=newarraylist();
}
公共作废addStockItem(StockItem项目){
增加库存(项目);
}
公共整数大小(){
返回库存。大小();
}
公共字符串toString(){
字符串结果=”;
用于(库存项目:库存)
结果+=item.toString()+“\n”;
返回结果;
}
公共布尔值isValidIndex(整数索引){
返回指数>=0&&index=this.stock.size())//检查此索引是否存在
return null;//从库存中删除项目并返回
其他的
返回此.stock.get(索引);
}
/**
*
*@param索引
*@如果索引无效,则返回null,否则在给定位置删除项
*索引并返回删除的项。
*/
公共库存项目移除(整数索引){
if(index<0 | | index>=this.stock.size())//检查此索引是否存在
return null;//从库存中删除项目并返回
其他的
返回此.stock.remove(索引);
}
/**
*排序,使用{@link StockItem#compareTo(StockItem)}
*无法使用java中的内置排序方法
*/
公共无效排序(){
}
我猜您希望按项目大小对项目进行排序。您可以使用任何现有的排序算法,例如,或。此外,还可以使用Collections module中的Java内置方法,该方法更快、更容易实现。您可以使用Collections.sort()方法假设您的类正在实现Comparable接口及其抽象方法compareTo()。您可以实现comapreTo()方法手动或仅使用Integer类来执行此操作。我使用了第二种方法。我正在使用需要排序的必要方法和字段构建StockItem类。库存代码如下所示:
import java.util.ArrayList;
import java.util.Collections;
public class Inventory {
private ArrayList<StockItem> stock;
public Inventory() {
stock = new ArrayList<StockItem>();
}
public void addStockItem(StockItem item) {
stock.add(item);
}
public int size() {
return stock.size();
}
public String toString() {
String result = "";
for(StockItem item: stock)
result+=item.toString()+"\n";
return result;
}
public boolean isValidIndex(int index) {
return index >=0 && index < stock.size();
}
/**
*
* @param index
* @return null if index is not valid, otherwise
* return item at that index
*/
public StockItem getItem(int index) {
if (index < 0 || index >= this.stock.size())// check if this index exists
return null; // removes the item the from stock and returns it
else
return this.stock.get(index);
}
/**
*
* @param index
* @return null if index is invalid, otherwise remove item at the given
* index and return the removed item.
*/
public StockItem remove(int index) {
if (index < 0 || index >= this.stock.size()) // check if this index exists
return null; // removes the item the from stock and returns it
else
return this.stock.remove(index);
}
/**
* sort, using {@link StockItem#compareTo(StockItem)}
* cannot use built-in sort method from java
*/
public void sort() {
Collections.sort(stock);
}
}
import java.util.ArrayList;
导入java.util.Collections;
公共类目录{
私人ArrayList股票;
公共库存(){
stock=newarraylist();
}
公共作废addStockItem(StockItem项目){
增加库存(项目);
}
公共整数大小(){
返回库存。大小();
}
公共字符串toString(){
字符串结果=”;
用于(库存项目:库存)
结果+=item.toString()+“\n”;
返回结果;
}
公共布尔值isValidIndex(整数索引){
返回指数>=0&&index=this.stock.size())//检查此索引是否存在
return null;//从库存中删除项目并返回
其他的
返回此.stock.get(索引);
}
/**
*
*@param索引
*@如果索引无效,则返回null,否则在给定位置删除项
*索引并返回删除的项。
*/
公共库存项目移除(整数索引){
if(index<0 | | index>=this.stock.size())//检查此索引是否存在
return null;//从库存中删除项目并返回
其他的
返回此.stock.remove(索引);
}
/**
*排序,使用{@link StockItem#compareTo(StockItem)}
*无法使用java中的内置排序方法
*/
公共无效排序(){
收集.分类(库存);
}
}
StockItem类的代码应类似于以下内容:
public class StockItem implements Comparable<StockItem>{
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return this.size;
}
public Integer getSizeNonPrimativeInt() {
return new Integer(this.getSize());
}
@Override
public int compareTo(StockItem item) {
return this.getSizeNonPrimativeInt().compareTo(item.getSizeNonPrimativeInt());
}
}
public类StockItem实现可比较{
私有整数大小;
公共无效设置大小(整型大小){
这个。大小=大小;
}
公共int getSize(){
返回此.size;
}
公共整数getSizeNonPrimativeInt(){
返回新整数(this.getSize());
}
@凌驾
公共整数比较(库存项目){
返回此.getSizeNonPrimativeInt().compareTo(item.getSizeNonPrimativeInt());
}
}
希望有此帮助;)您尚未粘贴StockItem类的代码。因此,我假设下面有一个示例类,您可以相应地更改代码。让我们假设您的StockItem类如下:
class StockItem{
private String itemName;
private double itemPrice;
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getItemPrice() {
return itemPrice;
}
public void setItemPrice(double itemPrice) {
this.itemPrice = itemPrice;
}
}
您提到不能使用集合中的内置sort()方法,但我想您可能会被指出,不能使用sort()方法,因为它的排序方式不是您想要的方式。您需要使用自定义排序条件定义一个comparator对象,并将其传递给内置sort()方法
公共类目录{
私人ArrayList股票;
//--您的其他代码段
静态类ListComparator实现Comparator{
@凌驾
公共整数比较(库存项目o1、库存项目o2){
如果(o1.getItemName().compareTo(o2.getItemName())>0){
返回1;
}
否则,如果(o1.getItemName().compareTo(o2.getItemName())您想比较什么以及根据哪些标准进行比较?您能告诉我们您尝试了什么以及结果是什么(与预期结果相比)?您需要将StackItem类与您想要的排序标准共享。(例如,您想让它们按StackItem的数量排序吗?)但是我们不能使用java中的内置排序方法,而且我们对java是新手,因此我对你刚才做的事情感到困惑;(我在你的
public class Inventory {
private ArrayList<StockItem> stock;
//-- your other code segments
static class ListComparator implements Comparator<StockItem>{
@Override
public int compare(StockItem o1, StockItem o2) {
if(o1.getItemName().compareTo(o2.getItemName())>0){
return 1;
}
else if(o1.getItemName().compareTo(o2.getItemName())<0){
return -1;
}
else
return 0;
}
}
public void sort() {
Collections.sort(stock, new ListComparator());
}
}