Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
为什么我的toString方法在java中的元素数组中返回null?_Java_Arrays_Null_Tostring - Fatal编程技术网

为什么我的toString方法在java中的元素数组中返回null?

为什么我的toString方法在java中的元素数组中返回null?,java,arrays,null,tostring,Java,Arrays,Null,Tostring,I包含“a b c d e”的数组。 然后使用反向方法将数组中的元素交换为“e d c b a” 我正在尝试使用以下toString方法打印数组中包含的项: public String toString() { String s = ""; if(isEmpty()) { s = "List is empty"; } for(int i = 0; i < numItems; i++) { s += items[

I包含“a b c d e”的数组。 然后使用反向方法将数组中的元素交换为“e d c b a”

我正在尝试使用以下toString方法打印数组中包含的项:

public String toString()
{
    String s = "";
    if(isEmpty())
    {
        s = "List is empty";
    }
    for(int i = 0; i < numItems; i++)
    {
        s += items[i] + " ";
    }//end for
    return s;
}//end of toString
公共字符串toString()
{
字符串s=“”;
if(isEmpty())
{
s=“列表为空”;
}
对于(int i=0;i
字符串s显示为“null e d c b”

我不知道为什么,但我认为必须将s初始化为“”

下面是我的反向方法,如果有帮助的话:

public void reverseList()
{
    for(int i = 0; i < items.length/2; i++)
    {
        Object temp = items[i];
        items[i] = items[items.length-i-1];
        items[items.length-i-1] = temp;
    }//end for
    System.out.println("List reversed");
}//end of reverseList
public void reverseList()
{
对于(int i=0;i
下面是数组的add方法:

public void add(int index, Object item)
throws  ListIndexOutOfBoundsException
{
    if (numItems > items.length)//fixes implementaion error
    {
        throw new ListException("ListException on add");
    }  // end if
    if (index >= 0 && index <= numItems)
    {
        // make room for new element by shifting all items at
        // positions >= index toward the end of the
        // list (no shift if index == numItems+1)
        for (int pos = numItems-1; pos >= index; pos--)
        {
            items[pos+1] = items[pos];
        } // end for
        // insert new item
        items[index] = item;
        numItems++;
        System.out.println("Item " + item + " inserted into position " + index + " in the list.");
    }
    else
    {
        System.out.println("Position specified is out of range!");
    }  // end if
} //end add
public void add(整数索引,对象项)
抛出ListIndexOutOfBoundsException
{
if(numItems>items.length)//修复了实现错误
{
抛出新的ListException(“添加时的ListException”);
}//如果结束,则结束
如果(索引>=0&&index=index)指向
//列表(如果索引==numItems+1,则无移位)
对于(int pos=numItems-1;pos>=index;pos--)
{
项目[pos+1]=项目[pos];
}//结束
//插入新项目
项目[索引]=项目;
numItems++;
System.out.println(“项”+项+”插入到列表中的“+索引+”位置。”);
}
其他的
{
System.out.println(“指定的位置超出范围!”);
}//如果结束,则结束
}//结束添加

谢谢!

问题很可能出在您的反向交换上,而不是您的toString方法上,因为反向交换已经正确地获得了EDCB,但如果被一个空值抵消的话


很抱歉,我的拼写反向测试失败。将s初始化为“”不会导致项目[0]输出为null。导致此输出的原因是相反的()您使用的方法是错误的。

请向我们证明您的数组包含这些元素。编辑您的问题。请不要在注释中发布代码。现在您已经演示了可以反转数组。但您仍然没有演示数组包含这些元素。@SotiriosDelimanolis这是否更具信息性?此时,我们正在调试您的代码。请原谅se自己做。或者提供一个完全可复制的,尽可能简短的,我们可以运行的例子来演示这个问题。EDBC在这里意味着什么?东唐卡斯特浸信会教堂(不得不用谷歌搜索)@jangroth对摩西的爱哈哈我不能停止laughing@Compass我已经确定它不是反向交换方法,因为我已经对它进行了调试。但是,当我在toString方法中逐行执行时,它将null赋值给了s@KickButtowski。数组中的元素是“a b c d e”。当数组反转时,元素的顺序是“e d c b a”。我编辑了文章以包含反转方法。我不认为它有任何问题,因为我测试了它,元素位于正确的位置。我认为您需要提供更多信息。我无法从您发布的代码中找到任何内容。