java将数据保存到数组更高效的代码
我对Android Studio非常陌生,已经编写了一个产品扫描应用程序,它将数据存储在一个数组中,确实可以工作,但我知道这段代码很长,我想知道如何提高效率的建议,我想重写它,这样我就不会有两个大块,一个要更新,一个要添加,我已经在这方面做了详尽的研究
我的数组称为products,currentProduct是一个类项
public void SavetoArray() {
//Get Time & Date
GetTodaysDate();
//check ALL field before saving
//if (CurrentDate == null) CurrentDate = todayDateTimeString;
//if indexpoint at end of array then we need to use ADD action to ADD ARRAY RECORD TO EOF
if(indexpoint == products.size()){
}
if (indexpoint == products.size()) {
products.add(indexpoint + UPC_FIELD_OFFSET, currentProduct.getmUPC());
products.add(indexpoint + DESC_FIELD_OFFSET, currentProduct.getmName());
products.add(indexpoint + UNITHEIGHT_FIELD_OFFSET, currentProduct.getmUnitHeight());
products.add(indexpoint + UNITWIDTH_FIELD_OFFSET, currentProduct.getmUnitWidth());
products.add(indexpoint + UNITDEPTH_FIELD_OFFSET, currentProduct.getmUnitDepth());
products.add(indexpoint + TRAYHEIGHT_FIELD_OFFSET, currentProduct.getmTrayHeight());
products.add(indexpoint + TRAYWIDTH_FIELD_OFFSET, currentProduct.getmTrayWidth());
products.add(indexpoint + TRAYDEPTH_FIELD_OFFSET, currentProduct.getmTrayDepth());
products.add(indexpoint + TRAYNUMH_FIELD_OFFSET, currentProduct.getmTrayNumHigh());
products.add(indexpoint + TRAYNUMW_FIELD_OFFSET, currentProduct.getmTrayNumWide());
products.add(indexpoint + TRAYNUMD_FIELD_OFFSET, currentProduct.getmTrayNumDeep());
products.add(indexpoint + TRAYTOTALNUM_FIELD_OFFSET, currentProduct.getmTrayTotalNum());
products.add(indexpoint + STATUS_FIELD_OFFSET, currentProduct.getmStatus());
products.add(indexpoint + DATE_FIELD_OFFSET, currentProduct.getmDate());
// this just checks we are adding a row to arraylist
Toast.makeText(this, "Products on file" + products.size() / RecordFieldCount, Toast.LENGTH_LONG).show();
} else {
//if indexpoint not end of array then we need to use set action = UPDATE ARRAY RECORD
products.set(indexpoint + UPC_FIELD_OFFSET, currentProduct.getmUPC());
products.set(indexpoint + DESC_FIELD_OFFSET,currentProduct.getmName());
products.set(indexpoint + UNITHEIGHT_FIELD_OFFSET, currentProduct.getmUnitHeight());
products.set(indexpoint + UNITWIDTH_FIELD_OFFSET, currentProduct.getmUnitWidth());
products.set(indexpoint + UNITDEPTH_FIELD_OFFSET, currentProduct.getmUnitDepth());
products.set(indexpoint + TRAYHEIGHT_FIELD_OFFSET, currentProduct.getmTrayHeight());
products.set(indexpoint + TRAYWIDTH_FIELD_OFFSET, currentProduct.getmTrayWidth());
products.set(indexpoint + TRAYDEPTH_FIELD_OFFSET, currentProduct.getmTrayDepth());
products.set(indexpoint + TRAYNUMH_FIELD_OFFSET, currentProduct.getmTrayNumHigh());
products.set(indexpoint + TRAYNUMW_FIELD_OFFSET, currentProduct.getmTrayNumWide());
products.set(indexpoint + TRAYNUMD_FIELD_OFFSET, currentProduct.getmTrayNumDeep());
products.set(indexpoint + TRAYTOTALNUM_FIELD_OFFSET, currentProduct.getmTrayTotalNum());
products.set(indexpoint + STATUS_FIELD_OFFSET, currentProduct.getmStatus());
products.set(indexpoint + DATE_FIELD_OFFSET, currentProduct.getmDate());
}
}
# 1 楼答案
下面是Oracle关于类/对象的文档,可以帮助您进行操作。 https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
# 2 楼答案
正如我所看到的,这更多的是一个代码设计的问题,而不是其他任何事情。你能做的就是重新设计你的代码
创建地图以显示偏移类型及其值(日期\字段\偏移,状态\字段\偏移,…)
而不是使用当前产品。getXX(),创建一个工厂。我们称之为CurrentProductFactory
您可以创建一个名为setUpdate(int-gap、int-index、int-retrievedValue)的方法。此方法根据indexpoint的值从集合或更新进行切换
您的代码变成: