【问题标题】:Null Pointer Exception, Cant figure out why? [closed]空指针异常,不知道为什么? [关闭]
【发布时间】:2019-03-16 08:03:22
【问题描述】:

我正在尝试编写此问题的代码。

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

str [0] = “1, 15, 2300
, str [1] = “15, 23, 1
00, and str [2] = “23, 1, 1500.

请注意,数字之间用 ‘,’,它们之间没有空格。 您需要创建一个名为 Shifts 的类,该类具有以下方法:

public  static String[]
giveShifts (LinkedList<Integer> list)

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

我尝试调试我的代码,但无济于事。它遍历所有节点,一旦到达最后一个节点,该值为空。我不太清楚为什么。

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

【问题讨论】:

标签: java nullpointerexception linked-list queue


【解决方案1】:

我不明白您显示的输出如何在字符串数组的每个元素中将数字之一乘以 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);
    }

}

从您的代码中提取,并且由于您可以使用队列,因此这里是改进的 giveShifts 方法

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

【讨论】:

  • 不幸的是,我只能使用堆栈或队列来实现这一点。我已经编辑了我的问题以使用堆栈。如果你能过去帮我解决这个问题!
  • 您的代码无法编译。如果您使用 java.util.LinkedList 作为参数,则不会存在诸如 length() 之类的方法或诸如 head 之类的成员变量。 StackInt 赔率 = new StackInt(list.size()); 之类的语句或节点 curr = list.head;似乎是指您尚未共享的课程。不正确的 API 使用和未知的类使您难以遵循您的代码。
  • 由于您可以使用队列,从代码中提取点点滴滴,我在解决方案中重新实现了您的方法 giveShifts。我使用 Java8 语法来减少代码行数。如果您需要 Java7 或更低版本的解决方案,您可以练习替换循环和 Lamdas
【解决方案2】:

将此行curr = curr.next 移动到循环的最后一行:

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

【讨论】:

  • 知道了!谢谢你。您还可以回顾一下我的代码的逻辑吗?我会很感激你的输入,因为我得到了错误的输出,在我们说话的时候试图调试它!虽然不再获得空指针。
  • 1.你不应该使用队列。队列中的数据一旦拉出就会丢失。您可以改用 ArrayList。 2. 'num++' 错了,你想要'queue' 中的第二个数字,但是'num++' 是queue 中第一个数字('num')的值加1。
  • 您使用的编程语言是什么?您的代码看起来像 Java 语言,但大部分代码对于 Java 来说是错误的。例如list.length() 错误,正确的形式是 list.size()。
  • for(int i=0; i
猜你喜欢
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多