【问题标题】:Is it possible to override an array which had the length of 0 when initialized?是否可以覆盖初始化时长度为 0 的数组?
【发布时间】:2019-02-18 21:09:14
【问题描述】:

我试图找出一种在这种情况下覆盖特定数组的方法。 我遍历 LinkedList,当我遇到 Element null 时,我应该将 Element 的索引添加到已经定义的整数数组中。

public int [] indexes()       
{
    int [] result = new int [0];
    int counter  = 0;
    Element current = first;
    int k = 0;

    for(int i = 0 ; i< size() ; i++)
    {
        if(current.getContent()==null)
        {
            counter++;     
        }
    }

    int [] temp = new int [counter];

    current = first;

    for(int i = 0 ; i< size() ; i++)
    {
        if(current.getContent()==null)
        {
            temp[k++] = i ;

        }
    }

    result = temp;
    return result;

如您所见,我有一个数组,其中没有元素,长度为 0。

之后我遍历列表以检查是否以及有多少元素为空。当我找到一个时,我会增加计数器。

我用计数器的长度设置了一个新的临时数组,并用索引填充临时元素。

第一个问题:结果线是否 = temp;按预期工作?名为 result 的数组现在是否包含 temp 数组?

第二个问题:如何在不出现 NullpointerException 的情况下检查 LinkedList 中的元素是否为空?我尝试在我的 if 条件中使用以下内容:

  1. 当前 == 空
  2. current.getContent() == null
  3. current.equals(null)
  4. current.getContent().equals(null)

所有这些都返回一个 0 的计数器,这意味着它在遇到空元素时没有增加计数器。如何重写 if 条件?

【问题讨论】:

  • 1) 没有名为result 的数组。有一个名为resultreference 变量,它可以引用一个数组。在result = temp 之前,它指的是一个大小为零的数组。在result = temp 之后,它引用了与temp 相同的数组。
  • 什么是first
  • First 只是 LinkedList 中的第一个元素。到数组的东西。有一个数组还是有?我的意思是我在方法的开头声明了它。它不应该覆盖它吗?
  • current 如果您从不将其推进到下一个元素,它的意义何在?现在,它总是指第一个元素。
  • 好的 current.getContent() == null 是要走的路。现在计数器实际上按预期递增。还要感谢您实际将当前指针设置为下一个元素的提示。不敢相信我忘记了。最后一个仍未解决的问题是如何覆盖结果数组?

标签: java arrays nullpointerexception null overriding


【解决方案1】:

问题 1:是的。 result 和 temp 引用相同的数据。

问题 2:你的 for 循环有问题:

for(int i = 0 ; i< size() ; i++)
{
    if(current.getContent()==null)
    {
        temp[k++] = i ;

    }
}

你没有改变当前,所以它总是指向第一个元素。

我不确定您是使用 Java 的 LinkedList 还是实现自己的。

我写了这个小程序。我认为它证明了您正在尝试做的事情:

import java.util.*;

public class Foo {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();;
        list.add("Hello");
        list.add("There");
        list.add(null);

        for (String str: list) {
            System.out.println(str != null ? str : "<null>");
        }

        ArrayList<Integer> indexes = new ArrayList<Integer>();
        int index = 0;
        for (String str: list) {
            if (str == null) {
                indexes.add(index);
            }
            ++index;
        }
        for (Integer thisIndex: indexes) {
            System.out.println(thisIndex);
        }
    }
}

最后,您需要将 ArrayList 转换为 int[]。但是这个小家伙的输出是:

$ javac Foo.java && java Foo
Hello
There
<null>
2

【讨论】:

  • 感谢您的回答。我设法解决了与您非常相似的问题!
【解决方案2】:

我能够用这一行覆盖结果数组。

result = temp;

检查列表中元素是否为空的方法也是:

current.getContent() == null;

最后我实际上忘记了将当前指针设置为下一个元素,这实际上是我最大的问题:

current = current.getSucc();

感谢大家的帮助和时间。我真的很感激!

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2014-07-25
    • 1970-01-01
    • 2010-12-12
    • 2014-10-24
    • 1970-01-01
    相关资源
    最近更新 更多