简体   繁体   中英

Iterate through array and assign each value

I need a reducer to iterate through an array and assign each element (2 elements total). But is my code taking all values and simply assigning it to the key?

For example here:

     public void reduce(Text key, Iterator<Text[]> values,
     OutputCollector<Text, Text[]> output, Reporter reporter)
     throws IOException {

Is this method just dumping all my array into the bucket of values?

IS there a way to use a While loop to take each element of the Text[] and assign it to an int?

Here is my start of that:

      while (values.hasNext()){
                String AtBats = values.toString();
                int AB = Integer.parseInt(AtBats);

This would take every value and make it an AtBat, but I want the second element to be a Hit. Confused on how to accomplish this.

As far as I understand from your response you want to take your values and then parse them into a string.

You have everything down but I would suggest for you to inline AtBats and just have it as

int AB = Integer.parseInt(values.toString());

UPDATE: After talking through this we came down to the conclusion that passing an array and looping through it is superfluous.

Instead of passing an array with two values, then using a while loop to go through it and assign its values to two variables just pass those values down independently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM