Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList-涉及比较_Java - Fatal编程技术网

Java ArrayList-涉及比较

Java ArrayList-涉及比较,java,Java,我将要解释的这个概念似乎不难讨论,但我会尽量给出更多细节 所以有两个班。其中一个用于设置setter和getter的属性,另一个文件是我的主文件,我在其中进行所有比较 我想在数组中进行比较,但要考虑的属性太多了。这就是它当前的样子: [[Year, Month, type1, type2, name, $], [Year, Month, type1, type2, name, $], [Year, Month, type1, type2, name, $]] 只有两个整数是$和year,其余的

我将要解释的这个概念似乎不难讨论,但我会尽量给出更多细节

所以有两个班。其中一个用于设置setter和getter的属性,另一个文件是我的主文件,我在其中进行所有比较

我想在数组中进行比较,但要考虑的属性太多了。这就是它当前的样子:

[[Year, Month, type1, type2, name, $], [Year, Month, type1, type2, name, $], [Year, Month, type1, type2, name, $]]
只有两个整数是$和year,其余的是字符串。 现在您已经了解了我正在使用的属性类型,下面是一个小片段,我正在使用它从Excel收集这些信息

ArrayList<customType> workList = new ArrayList<customType>();
 for (int i = start; i <= end; i++) {
     Row r = sheet.getRow(i);
    ArrayList tmp = new ArrayList();
      tmp.setYear((int) row.getCell(0).getNumericCellValue());
      tmp.setMonth(row.getCell(1).getStringCellValue());
      tmp.setType1(row.getCell(2).getStringCellValue());
      tmp.setType2(row.getCell(3).getStringCellValue());
      tmp.setName(row.getCell(4).getStringCellValue());
      tmp.setPrice((int) row.getCell(5).getNumericCellValue());

    workList.add(temp);
 }
ArrayList工作列表=新建ArrayList();

对于(int i=start;i一种更有意义的方法是使用一个真正的数据库,如MySQL,您可以使用SQL轻松地查询、排序和组织。这将为您想对这些数据做的任何事情提供简单的解决方案。您迄今为止选择的道路只会导致疯狂和绝望。

一种可以使更明智的做法是使用真实的数据库,如MySQL,您可以使用SQL轻松地查询、排序和组织。这将为您想要对这些数据执行的任何操作提供简单的解决方案。您迄今为止选择的路径只会导致疯狂和绝望。

如果我是您,我会从更好地分解对象开始。什么时候你把你的系统分解成更多的逻辑组件——他们可以真正开始为你做繁重的工作。
public class Game
{
  private String name;
  private String category;
  private Map<String, Double> pricesByDate;

  // Constructors, getters, setters

  public void addPrice(String monthYear, Double price)
  {
     pricesByDate.put(monthYear, price);
  }

  public Double getPrice(String monthYear)
  {
    // here, let the object do the work for you
    Double price = pricesByDate.get(monthYear);
    return price != null ? price : 0.0;
  }
}
公共类游戏
{
私有字符串名称;
私有字符串类别;
私人地图价格;
//构造函数、getter、setter
公共无效附加价格(字符串月,双倍价格)
{
价格(每月,价格);
}
公共双getPrice(字符串月)
{
//在这里,让对象为您完成工作
双倍价格=pricesByDate.get(每月);
退货价格!=null?价格:0.0;
}
}
因此,与其担心到处都填充0,不如让对象足够智能,以便在找不到数据时报告0


现在改变你的解析器来构造一个游戏列表。

如果我是你,我会先把我的对象分解得更好一点。当你把你的系统分解成更多的逻辑组件时,他们可以真正开始为你做重启。考虑下面的内容:

public class Game
{
  private String name;
  private String category;
  private Map<String, Double> pricesByDate;

  // Constructors, getters, setters

  public void addPrice(String monthYear, Double price)
  {
     pricesByDate.put(monthYear, price);
  }

  public Double getPrice(String monthYear)
  {
    // here, let the object do the work for you
    Double price = pricesByDate.get(monthYear);
    return price != null ? price : 0.0;
  }
}
公共类游戏
{
私有字符串名称;
私有字符串类别;
私人地图价格;
//构造函数、getter、setter
公共无效附加价格(字符串月,双倍价格)
{
价格(每月,价格);
}
公共双getPrice(字符串月)
{
//在这里,让对象为您完成工作
双倍价格=pricesByDate.get(每月);
退货价格!=null?价格:0.0;
}
}
因此,与其担心到处都填充0,不如让对象足够智能,以便在找不到数据时报告0


现在更改解析器以构造游戏列表。

`ArrayList tmp=new ArrayList();`should是
customType tmp=new customType;
right?`ArrayList tmp=new ArrayList();`should是
customType tmp=new customType;
对吗?哈哈,我知道。我的问题其实很小,在我发现错误后变得很痛苦,不幸的是,这是完成它的唯一方法。我可以在程序中内置一个脱机SQL数据库吗?事实上,是的——签出,这是一个流行的100%Java数据库引擎这可以在进程中使用内存表。谢谢。我不用它就完成了,但下次我将使用它。:)哈哈,我知道。我的问题其实很小,在我发现一个错误后,它变得很痛苦,不幸的是,这是完成它的唯一方法。我可以在程序中内置一个离线SQL数据库吗?事实上,是的——检查一下,这是一个流行的100%Java数据库引擎,可以在进程中处理内存中的表。谢谢。我得到了不用它就可以了,但下次我会用它。:)类型呢?我忘了提到游戏可以放在哪里:游戏射手HaloReach和游戏种族HaloReach(虽然这不是真的,但在这个例子中是真的。)而且,价格不会只显示为0,字段也不会出现,这正是我想复制名称并以0价格改为前几个月的原因。我知道这听起来既垃圾又复杂。。或者我解释得不对。但是你的主意听起来好多了,我只是想再多帮点忙。再次感谢。那类型呢?我忘了提到游戏可以放在哪里:游戏射手HaloReach和游戏种族HaloReach(虽然这不是真的,但在这个例子中是真的。)而且,价格不会只显示为0,字段也不会出现,这正是我想复制名称并以0价格改为前几个月的原因。我知道这听起来既垃圾又复杂。。或者我解释得不对。但是你的主意听起来好多了,我只是想再多帮点忙。再次感谢。