Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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中的最后一块数据。布尔标记还是无边界索引?_Java_Arrays - Fatal编程技术网

正确处理Java中的最后一块数据。布尔标记还是无边界索引?

正确处理Java中的最后一块数据。布尔标记还是无边界索引?,java,arrays,Java,Arrays,我有一个数组列表,包含(I)ndex,(C)category,(S)ubcategory,(V)value I C S V 011A 1b 21c 3 1 3 D 4 1 3 E 5.2.1 F 6.2.1克 7.2小时 8 2 J 我需要处理AB,然后是C,然后是DE。然后处理AB+C+DE,重复操作2,以此类推。最后,使用以下命令编写输出文件: AB+C+DE FG+HJ 请不要将*和+文字解释为乘积和总和 public void translate(List<String[]&

我有一个数组列表,包含(I)ndex,(C)category,(S)ubcategory,(V)value

I C S V
011A
1b
21c
3 1 3 D
4 1 3 E
5.2.1 F
6.2.1克
7.2小时
8 2 J

我需要处理AB,然后是C,然后是DE。然后处理AB+C+DE,重复操作2,以此类推。最后,使用以下命令编写输出文件:

 AB+C+DE
 FG+HJ
请不要将
*
+
文字解释为乘积和总和

public void translate(List<String[]> raw) {

    int oldCategory = 0;
    int newCategory = 0;
    int oldSubCategory = 0;
    int newSubCategory = 0;        

    boolean keepWorking = true;     
    do {        
        oldCategory = newCategory;
        newCategory = getNewCategory(oldCategory, raw); //get end of category. Now it returns 5, then 9.            
         do {               
            oldSubCategory = newSubCategory;    
            newSubCategory = getNewSubCategory(oldSubCategory, raw);            
            List<String> products = doFirstOperation(raw, oldCategory, newCategory, oldSubCategory, newSubCategory); //A*B, D*E, etc.

            } while (newSubCategory< newCategory);              
            doSecondOperation(products); // compute "A*B+C+D*E" and append it to file

        }   while (newCategory < raw.size());           
    }           
public void translate(原始列表){
int oldCategory=0;
int newCategory=0;
int oldSubCategory=0;
int newSubCategory=0;
布尔keepWorking=true;
做{
旧类别=新类别;
newCategory=getNewCategory(oldCategory,raw);//获取类别的结尾。现在它返回5,然后返回9。
做{
oldSubCategory=newSubCategory;
newSubCategory=getNewSubCategory(旧子类别,原始);
List products=doFirstOperation(原始、旧类别、新类别、旧子类别、新子类别);//A*B、D*E等。
}while(newSubCategory
getNewCategory(oldCategory,raw)
返回新类别或数组中第一个元素的索引。如果当前类别是最后一个,则返回size()。 在当前设置中,我将newSubCategory路由设置在类别范围之外。这是在要求一个ArrayIndexOutOfBoundsException。我可以使用标记
布尔isLastCategory
,do while(!isLastCategory)并返回newCategory作为给定类别中最后一个元素的索引,但这看起来很庞大


组织这样的代码的好方法是什么

这种模式在某些地方被称为“级联报告中断”。以下是一种可能的方法,使用伪代码:

curCat = null
curSub = null
while rec = read()
    if (rec.cat != curCat)
        if (curCat != null)
            endCat()
        startCat()
    else if (rec.sub != curSub)
        endSub()
        startSub()
    accumulate rec
end while
if (curCat != null)
    endCat()

function startCat()
    curCat = rec.cat
    initialize cat accumulator
    startSub()

function startSub()
    curSub = rec.sub
    initialize sub accumulator

function endSub()
    finalize subcategory

function endCat()
    endSub()
    finalize category
如果您不喜欢空检查是浪费的(它们只适用于文件的开头),您可以使用以下替代方法:

rec = read()
if (!EOF)   
    startCat() 
    while rec = read()
        if (rec.cat != curCat)
            endCat()
            startCat()
        else if (rec.sub != curSub)
            endSub()
            startSub()
        accumulate rec
    end while
    endCat()
根据需要调整OOP特性和封装


顺便说一句,这可以追溯到20世纪60年代和COBOL报告编写程序,但仍然是一种将嵌套中断和累积概念化的简洁方法。

如果您使用的是Java8,下面是实现这一点的方法

    List<Model> models = new ArrayList<>();
    models.add(new Model("0", "1", "1", "A"));
    models.add(new Model("1", "1", "1", "B"));
    models.add(new Model("2", "1", "2", "C"));
    models.add(new Model("3", "1", "3", "D"));
    models.add(new Model("4", "1", "3", "E"));
    models.add(new Model("5", "2", "1", "F"));
    models.add(new Model("6", "2", "1", "G"));
    models.add(new Model("7", "2", "2", "H"));
    models.add(new Model("8", "2", "2", "J"));

    Map<String, Map<String, List<Model>>> map = models.stream()
            .collect(groupingBy(Model::getCategory, groupingBy(Model::getSubCategory)));

    List<String> strings = map.values().stream()
            .map(submap -> submap.values().stream()
                    .map(list -> list.stream().map(Model::getValue).collect(joining("*"))).collect(joining("+")))
            .collect(toList());

    System.out.println(strings);

那么您的代码当前是否正常工作,或者您是否遇到了
ArrayIndexOutOfBounds
异常?我的问题是关于中断跟踪的最佳设计模式。这与本地bug无关。显然,“级联报告中断”是一条路要走。
[A*B+C+D*E, F*G+H*J]