如何改进这个扩展阵列? [英] How do I improve this expanding array?

查看:91
本文介绍了如何改进这个扩展阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是练习。



在本练习中,您将有机会改进我们的扩展阵列以添加一些您在ArrayList中获得的其他功能。



您应该添加三种方法:



public void add( int index,int element)

public int remove(int index)

public int size()



////////////////////////////////////////////////// ////////////////////////////////////



这是代码:

This is the exercise.

In this exercise you’ll get a chance to improve our expanding array to add some of the other functionality that you get in ArrayList.

You should add three methods:

public void add(int index, int element)
public int remove(int index)
public int size()

//////////////////////////////////////////////////////////////////////////////////////

This is the code:

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
        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   
    }
    
    // Return the number of elements in your array.
    public int size()
    {
        // your code here
        return 0;
    }
    
    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; i++) {
            str += arr[i] + ", ";
        }
        if (str.length() > 0 && str.charAt(str.length()-2)==',') {
            str = str.substring(0, str.length()-2);
            str += "}";
        }
        return str;
    }
}





我的尝试:



我没有尝试任何东西。



What I have tried:

I havent tried anything.

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。

首先看看它做了什么,并将其与ArrayList进行比较。这应该告诉你你缺少什么以及你应该添加什么。



亲自尝试一下,你可能会发现它并不像你想象的那么困难!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
So start by looking at what that does, and compare that to an ArrayList. That should tell you what you are missing and what you should add.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


引用:

我没有尝试过任何东西。

I havent tried anything.

然后尝试一下。



例如, size 方法实现是微不足道的。 />
另外两种方法,即 add remove 更难,无论如何你知道,<我欢迎i>具体的问题。

Then try something.

For instance, the size method implementation is trivial.
The other two methods, namely add and remove are more difficult, anyway you know, specific question are welcomed here.


Quote:

我没有尝试过任何事情。

I havent tried anything.



首先要看的是阅读文档。


First thing to try is read documentation.

Quote:

我们的老师也不能这样做。

Our teacher cant do it either.



然后你的老师没资格。



我们不做你的家庭作业。< br $>
HomeWork不会在乞求其他人做你的工作时测试你的技能,它会让你思考并帮助你的老师检查你对你已经学过的课程以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请展示您的代码并解释这个问题,我们可能会提供帮助。


Then your teacher is not qualified.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.


这篇关于如何改进这个扩展阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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