Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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_Nullpointerexception_Linked List_Queue - Fatal编程技术网

Java 空指针异常,无法找出原因?

Java 空指针异常,无法找出原因?,java,nullpointerexception,linked-list,queue,Java,Nullpointerexception,Linked List,Queue,我正试图对这个问题进行编码 假设给您一个由n个节点组成的链表,每个节点存储一个整数。编写一个高效的Java代码,以n个字符串的数组打印链表中奇数的所有循环移位。 例如,如果列表为1→ 2.→ 15→ 14→ 23,输出将是字符串的数组str,以便 请注意,数字之间用空格分隔 “,”并且它们之间没有空间。 您需要创建名为Shifts的类,该类具有以下方法: 。这里的列表是一个维护整数的链表 我试着调试我的代码,但没用。它遍历所有节点,一旦到达最后一个节点,该值为null。我不太清楚为什么 impo

我正试图对这个问题进行编码

假设给您一个由n个节点组成的链表,每个节点存储一个整数。编写一个高效的Java代码,以n个字符串的数组打印链表中奇数的所有循环移位。 例如,如果列表为1→ 2.→ 15→ 14→ 23,输出将是字符串的数组str,以便

请注意,数字之间用空格分隔 “,”并且它们之间没有空间。 您需要创建名为Shifts的类,该类具有以下方法:

。这里的列表是一个维护整数的链表

我试着调试我的代码,但没用。它遍历所有节点,一旦到达最后一个节点,该值为null。我不太清楚为什么

import java.util.Arrays;

public class Shifts
{
    public static String[] giveShifts(LinkedList<Integer> list)
    {
        String[] result = new String[list.length()];
        StackInt odds = new StackInt(list.length());
        Node curr = list.head;

        while(curr != null)
        {
            if ((Integer)curr.getValue() % 2 == 0)
            {
                odds.push(((int)curr.getValue()));

            }
            curr = curr.next;
        }


            int[] nums = new int[list.length()];
            String numsStr = "";

            while(!odds.isEmpty())
            {
            for(int i=0; i<result.length; i++)
            {
             nums[i] = odds.pop();
            }

            numsStr = Arrays.toString(nums);

            for(int j=0; j<result.length; j++)
            {
                result[j] = numsStr;
            }
        }
        return result;
    }
}
将此行curr=curr.移动到循环最后一行的旁边:

while(curr != null)
{
    if ((int)curr.getValue() % 2 == 0)
    {
        odds.enqueue(((int)curr.getValue()));
    }
    curr = curr.next;
}

我不明白您显示的输出是如何在字符串数组的每个元素中有一个数字乘以100的。根据我对你问题的理解,这里有一个解决办法

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);
    }

}
从您的代码中提取,并且由于您被允许使用队列,这里是一个改进的方法

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;
    }

你的缩进到处都是,这使得你的代码很难阅读。也许纠正这一点也会发现任何错误。@GolezTrol完成。对不起,我是新使用stackoverflow的!可能是重复的,我明白了!非常感谢。你能不能把我的代码的逻辑解释一下?我会很感激你对它的输入,因为我得到了错误的输出,试图在我们说话的时候调试它!但是不再获取空指针。1。你不应该使用队列。队列中的数据一旦拉出就会丢失。您可以改用ArrayList。2. 'num++'是错误的,您想要“队列”中的第二个数字,但“num++”是队列中第一个数字的值“num”加1。您使用的编程语言是什么?您的代码看起来像Java语言,但大多数代码对Java来说都是错误的。e、 g.list.length错误,正确形式为list.size.forint i=0;不幸的是,我只能使用堆栈或队列来实现这一点。我编辑了我的问题以使用堆栈。如果你能过去帮我解决这个问题!您的代码无法编译。如果您使用java.util.LinkedList作为参数,那么像length这样的方法或像head这样的成员变量就不存在了。StackInt赔率=new StackIntlist.size等语句;或节点curr=list.head;似乎是指你没有分享的课程。不正确的API使用和未知的类使您很难理解代码。因为您可以使用队列,从代码中提取一些细节,我已经在解决方案中重新实现了您的方法。我使用了Java8语法来减少代码行数。如果需要Java7或更低版本的解决方案,您可以练习替换循环和Lamdas
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);
    }

}
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;
    }