[Solved] Null Pointer Exception, Cant figure out why? [closed]


I did not understand how the output you showed has one of the numbers multiplied by 100 in each element of String array. Based on my understanding of your question here is a solution.

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Shifts {

    public static void main(String[] args) {
        LinkedList<Integer> list = Arrays.asList(1, 2, 15, 14, 23)
                .stream().filter(i -> i % 2 != 0)
                .collect(Collectors.toCollection(LinkedList::new));
        int oddCount = list.size();
        String[] results = new String[oddCount];
        IntStream.range(0, oddCount).forEach(index -> {
            Collections.rotate(list, 1);
            results[index] = list.stream()
                             .map(i -> i.toString())
                             .collect(Collectors.joining(","));
        });
        Arrays.asList(results).stream().forEach(System.out::println);
    }

}

Drawing from your code, and since you are allowed to use Queue, here is refined giveShifts method

public static String[] giveShifts(LinkedList<Integer> list) {
        //since you need only odd numbers, filter out even ones
        list = list.stream().filter(i -> i % 2 != 0)
                .collect(Collectors.toCollection(LinkedList::new));
        int oddCount = list.size();
        String[] result = new String[oddCount];
        //type cast it to Queue as it will help understand the concept
        // shift is essentially, remove from head and add to tail in queue
        java.util.Queue<Integer> queue = list;
        for(int i=0;i<oddCount; i++){
            queue.add(queue.remove());
            result[i] = queue.stream()
                       .map(element -> element.toString())
                       .collect(Collectors.joining(","));
        }
        return result;
    }

3

solved Null Pointer Exception, Cant figure out why? [closed]