English 中文(简体)
iterating over map and array simultaneously in a for loop
原标题:

I am having some trouble creating a for loop within a constructor to iterate over a map and an array at the same time. Here, it is indicated that this cannot be done with an enhanced for loop.

I have something like this, which raises a compiler error. Basically, the class has a Map which I want to populate via the constructor which takes a collection and a variable number of integers as parameters.

The var-arg expression evaluates to an array of integers, so I tried to put both enhanced iterators in the same loop, but didn t work.

private final Map<Module, Integer> modules = new HashMap<Module, Integer>();    
    AssemblyType(Collection<Module> modules, int... units) {
        int i = 0;
        for (Module module : modules, int i : units) {
            this.modules.put(module, units[i]);
        }       
    }

Thanks for any ideas on how to proceed.

最佳回答

The naive way to do this would just be to track i yourself:

private final Map modules = new HashMap();    
    AssemblyType(Collection modules, int... units) {
        int i = 0;
        for (Module module : modules) {
                this.modules.put(ingredient, units[i]);
                i++;
        }               
    }

I m not sure if there s a better way, but I m pretty sure you can t combine two iterators inside a single for loop like your original example.

问题回答

It looks like your trying to pass a the modules Map as a Collection which would cause a compile error.

Code to iterate over both could be something like this

public MyMethod(Map<Object, Object> objectMap, Integer ... intArray) {
    if( intArray.length != ObjectMap.size() ) {
       //However you want to handle this case
    }
    Iterator<Object> mapKeyIterator = objectMap.keySet().iterator();
    Iterator<Integer> integerIterator = Arrays.asList(intArray).iterator();

    while(mapKeyIterator.hasNext()) { //If the array and map are the same size then you only need to check for one.  Otherwise you ll need to validate both iterators have a next
        Object keyFromMap = mapKeyIterator.next();
        Object valueFromMap = objectMap.get(keyFromMap);
        Integer intFromArray = integerIterator.next();
        //Whatever you want to do
   }
}

If you know they are the same length then you could also traverse the array with a for(int i ... loop and just use an iterator for the map if you didn t want to create a List.

This seems like a very error-prone API, to ask for a completely disconnected map and array. Not just error-prone, but it will be hard for readers of the code to be able to tell which int goes with which map entry. I always advise against this kind of API. Try a Map<Object, MySpecialType> where MySpecialType aggregates both an Object and an int.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签