创建Java ArrayList数据结构

Certain data structures in Java can be created by you (yes you). In this example, we’ll go ahead and create an ArrayList data structure that has some of the methods that the built-in ArrayList class has.

您可以创建Java中的某些数据结构(是的)。 在此示例中,我们将继续创建一个ArrayList数据结构,该结构具有内置ArrayList类所具有的一些方法。

We’ll create 2 constructors:

我们将创建2个构造函数:

  • The default constructor that creates an ArrayList with a default size of 10.

    创建默认值为10的ArrayList的默认构造函数。
  • Constructor that allows an initial size to be passed to the array.

    允许将初始大小传递给数组的构造函数。

We’ll also create a number of methods:

我们还将创建许多方法:

  • void add(Object x); A method that allows you to place an Object at the end of the ArrayList.

    void add(Object x); 一种允许您将对象放置在ArrayList末尾的方法。

  • void add(int index, Object x); A method that allows you to place a value at a given location.

    void add(int index,Object x); 一种允许您在给定位置放置值的方法。

  • Object get(int index): Allows you to retrieve a value of the arrayList array from a given location.

    Object get(int index):允许您从给定位置检索arrayList数组的值。

  • int size(); Allows you to get the number of elements currently in the Arraylist.

    int size(); 允许您获取当前在Arraylist中的元素数。

  • boolean isEmpty(); Tests to see if the Arraylist is empty.

    boolean isEmpty(); 测试以查看Arraylist是否为空。

  • boolean isIn(Object x); A method that sees if a particular object exist in the arrayList.

    boolean isIn(Object x); 一种查看arrayList中是否存在特定对象的方法。

  • int find(Object x); Returns the location of first occurrence of an Object starting from location 0.

    int find(Object x); 返回从位置0开始的Object首次出现的位置。

  • void remove(Object x); Removes the first occurrence of an Object starting from location 0.

    void remove(Object x); 从位置0开始删除第一次出现的Object。

I encourage you not to look at the ArrayList built-in class. See if you can figure it out on your own. The only other restriction will be to store the Objects in an array data field. Create a test class to test the ArrayList class. Name your ArrayList class ArrayList.java so that it overwrites the built in class.

我鼓励您不要看ArrayList内置类。 看看是否可以自己解决。 唯一的其他限制是将对象存储在数组数据字段中。 创建一个测试类以测试ArrayList类。 将您的ArrayList类命名为ArrayList.java,以便它覆盖内置类。

Read the notes above each method. There is a precondition that states the requirements that you’ll need to abide by before the method is called. For example, if the method requires an array of integers to be passed to it, you will need to have an array of integers ready. After the method is called, there is the post-condition. This explains what the expected output of the method will be and the steps the method takes to achieve that output.

阅读每种方法上方的注释。 有一个前提条件规定您在调用该方法之前必须遵守的要求。 例如,如果该方法要求将整数数组传递给它,则需要准备好整数数组。 调用该方法后,存在后置条件。 这解释了该方法的预期输出是什么以及该方法为实现该输出所采取的步骤。

We’ll start off with the ArrayList class. The methods that were outlined above will be added to this class.

我们将从ArrayList类开始。 上面概述的方法将添加到此类中。

What good is the code if you’re not going to test it. We’ll create a Driver class that’s going to test the ArrayList code.

如果您不打算对其进行测试,那么代码有什么用呢? 我们将创建一个Driver类,该类将测试ArrayList代码。

And that’s really all their is too it. With basic Java skills, you too can create your ArrayList data structure.

这实际上就是他们的全部。 凭借基本的Java技能,您也可以创建ArrayList数据结构。

翻译自: https://medium.com/dev-genius/creating-the-java-arraylist-data-structure-3df0b18ad43b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值