这种方法有什么问题? [英] What's wrong with this method?

查看:62
本文介绍了这种方法有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject社区,



我正在处理一个问题,要求我编写一个方法来插入元素元素在索引索引的数组 arr 中。我的代码如下:



评分系统使用随机元素,数组和索引,但每次运行我返回的数组时都会错过一个元素。以下是评分结果之一:



Hello Codeproject community,

I'm working on a problem asking me to write a method to insert a element element into an array arr at the index index. My code attached below:

The grading system uses a randomized element, array, and index, but every time it runs the array that I return seems to miss an element. Here's one of the grading results:

Expected result: 
{11, 10, 78, 59, 35, 46, 84, 84, 34, 90}

Your result: 
{11, 10, 78, 59, 35, 46, 84, 84, 34}





谢谢



我的尝试:





Thank you

What I have tried:

public void add(int index, int element)
    {
        int tempArr[] = new int[arr.length + 1];

        for(int i = 0; i < index; i++)
        {
            tempArr[i] = arr[i];
        }
        tempArr[index] = element;
        for(int i = index; i < arr.length; i++)
        {
            tempArr[i + 1] = arr[i];
        }
        arr = tempArr;
    }





完整源代码:



Full source code:

<pre>public class ExpandingArray
{
    private static final int STARTING_SIZE = 10;
    private int[] arr;
    private int currentSize;
    private int numElements;
    
    public ExpandingArray()
    {
        arr = new int[STARTING_SIZE];
        currentSize = STARTING_SIZE;
        numElements = 0;
    }
    
    // Remove the element at index `index` and shift
    // all subsequent elements to the left. 
    public int remove(int index)
    {
        // your code here
        int tempArr[] = new int[currentSize - 1];
        for(int i = 0; i < index; i++)
        {
            tempArr[i] = arr[i];
        }
        for(int i = index + 1; i < currentSize; i++)
        {
            tempArr[i - 1] = arr[i];
        }
        currentSize--;
        arr = tempArr;
        return 0;
    }
    
    // Add the int `element` at the `index` in the array.
    // You'll need to shift everything one index to the right
    // after this index.
    public void add(int index, int element)
    {
        // your code here
        int tempArr[] = new int[arr.length + 1];

        for(int i = 0; i < index; i++)
        {
            tempArr[i] = arr[i];
            System.out.println(arr[i]);
        }
        tempArr[index] = element;
        System.out.println(tempArr[index]);
        for(int i = index; i < arr.length; i++)
        {
            tempArr[i + 1] = arr[i];
            System.out.println(arr[i]);
        }
        arr = tempArr;
    }
    
    // Return the number of elements in your array.
    public int size()
    {
        return currentSize;
    }
    
    private boolean isFull()
    {
        return numElements == currentSize;
    }
    
    private void expand()
    {
        System.out.println("Expanding");
        int newSize = currentSize * 2;
        int[] newArray = new int[newSize];
        
        // Copy over old elements
        for(int i = 0; i < currentSize; i++)
        {
            newArray[i] = arr[i];
        }
        
        currentSize = newSize;
        arr = newArray;
    }
    
    public int get(int index)
    {
        return arr[index];
    }
    
    public void add(int x)
    {
        if(isFull())
        {
            expand();
        }
        arr[numElements] = x;
        numElements++;
    }
    
    public String toString()
    {
        String str = "{";
        for (int i=0; i < numElements - 1; i++) {
            str += arr[i] + ", ";
        }
        if (str.length() > 0 && str.charAt(str.length()-2)==',') {
            str = str.substring(0, str.length()-2);
            str += "}";
        }
        return str;
    }
}

推荐答案

你的toString函数不正确。如果 arr 只有一个项目,那么该函数将抛出

your toString function is incorrect. If the arr has only one item, then the function will throw
java.lang.StringIndexOutOfBoundsException: String index out of range: -1





这里应该怎么做;

如果阵列中什么也没有打印

{}

如果只有一个元素打印

{element1}

如果不止一个那么打印

{element1,element2}

所以,建议的算法将是



here is how it should go;
if there is nothing in the array then print
{}
if one element only then print
{element1}
if more than one then print
{element1, element2}
so, suggested algorithm will be

string := "{"
if element count is zero then
  string := concat(string, "}");
  return
end if

string := concat(string, arr[0]);
while ( item in array, skipping the first element ): do
  string := concat(string, ", ", item);
end while

string := concat(string, "}")



尝试使用源代码


try your source code


为什么打印所有元素而不是最后一个元素?

Why are you printing all elements but the last one?
for (int i=0; i < numElements - 1; i++) {



当你添加元素时,它看起来所有变量都不会更新。



有一个工具可以让你看到你的代码正在做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪个期望与现实相符。

当你不这样做时了解你的代码正在做什么或为什么它做它做的事情,答案是调试器

使用调试器来查看你的代码正在做什么。只需设置一个断点和看到你的代码正在执行,调试器允许你逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging -your-first-java-application.html [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,只是帮助你到。当代码没有达到预期的效果时,你就会接近一个错误。


It also look that all variables are not update"d when you add an element.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于这种方法有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆