简体   繁体   English

迭代矩阵的元素

[英]Iterating through elements of a matrix

I'm writing a custom hashmap implementation that is backed by a matrix (2D array). 我正在编写一个由矩阵(2D数组)支持的自定义hashmap实现。 I want to iterate through the elements and print them, but want to avoid printing null elements. 我想迭代元素并打印它们,但是想避免打印null元素。

The hashmap only supports Integers and calculates the hashcodes as input mod 10. It will store these Integers inside the appropriate hashcode index, and collisions are resolved by iterating through the subarray and placing the Integer in the next available index of the subarray. hashmap仅支持Integers并将哈希码计算为输入mod 10.它将这些Integers存储在适当的哈希码索引中,并通过迭代子流并将Integer放在子流的下一个可用索引中来解决冲突。 If the maximum index of a given subarray is exceeded, it will create a new array of size 1.5x the original and copy the elements over to that. 如果超过给定子数组的最大索引,它将创建一个大小为原始数组1.5x的新数组,并将元素复制到该数组。

The problem is that System.out.print will throw an ArrayIndexOutOfBoundsException every time when iterator is equal to the subarray length, so the while loop is not working as expected. 问题是每当iterator等于子数组长度时, System.out.print将抛出一个ArrayIndexOutOfBoundsException ,因此while循环不能按预期工作。

I've already tried changing the while loop such that the iterator variable is supposed to be less than the maximum index of each subarray. 我已经尝试更改while循环,以便迭代器变量应该小于每个子数组的最大索引。 Changing the condition to iterator < array[i].length - 1 will avoid the exception but Java will not print the last element of each subarray. 将条件更改为iterator < array[i].length - 1将避免异常,但Java不会打印每个子数组的最后一个元素。

public void print () {
        int iterator = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == null)
                continue;
            System.out.print("[");
            while (array[i][iterator] != null && iterator < array[i].length) {
                System.out.print(array[i][iterator] + ", ");
                iterator++;
            }
            System.out.println ("]");
            iterator = 0;
        }
    }

The output is supposed to be something like 输出应该是这样的

[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, ]
[1, 11, 21, 31, 41, 51, 61, 71, 81, 91, ]...

But the actual output is 但实际产量是

[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at regularexpression.HashMap.print(HashMap.java:42)

将检查顺序反转为while(迭代器<array [i] .length && array [i] [iterator]!= null){...}

Changing this line should fix it: 更改此行应修复它:

     while (array[i][iterator] != null && iterator < array[i].length) {

to this: 对此:

     while (iterator < array[i].length && array[i][iterator] != null) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历哈希图并访问其元素 - iterating through hashmaps and accessing its elements 迭代遍历数组的所有元素时出现ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException when iterating through all the elements of an array 遍历jsoup中的元素并解析href - Iterating through elements in jsoup and parsing href 迭代数据结构的元素而不是Collection - Iterating through elements of a data structure instead of Collection 通过NxN矩阵迭代以形成具有特定规则的单词 - Iterating through a NxN matrix to form words with specific rule 如何加快这段代码? 微积分迭代矩阵的行和列 - how to speed up this code? calculus iterating through rows and cols of a matrix 遍历ArrayList并动态删除和添加特定索引处的元素 - Iterating through an ArrayList and also dynamically removing and adding elements at a specific index Java ConcurrentModificationException:是否可以在迭代时向哈希表添加元素? - Java ConcurrentModificationException: Is it possible to add elements to a hashtable while iterating through it? 遍历Java列表时如何展望未来的&#39;n&#39;元素? - how to look 'n' elements ahead while iterating through java list? 遍历列表时计算并删除列表中的相似元素 - Count and remove similar elements in a list while iterating through it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM