我想在2d数组上添加一行

时间:2021-04-04 21:33:06

The method assigned in the assignment says:

作业中指定的方法说:

boolean addLineSegment(int [] segment) - add a line segment to the database if its coordinates represent a valid line segment. This should increase the size of the lineSegment array by one and add the given line segment to the end. The method returns true if a line segment was added and false otherwise. The input should be an array of size 4.

boolean addLineSegment(int [] segment) - 如果线段表示有效的线段,则将线段添加到数据库。这应该将lineSegment数组的大小增加1,并将给定的线段添加到结尾。如果添加了线段,则该方法返回true,否则返回false。输入应该是大小为4的数组。

I'm kind of stuck because I want to add a row into my array lineSegments[][] without having to reallocate it and erasing the previous contents of the array. How do I keep the contents of the array and add a new row to it so I can add the contents of segment[] to lineSegments[][]?

我有点卡住,因为我想在我的数组lineSegments [] []中添加一行,而不必重新分配它并删除数组的先前内容。如何保留数组的内容并向其添加新行,以便我可以将segment []的内容添加到lineSegments [] []?

2 个解决方案

#1


4  

Use Java ArrayUtils static methods, there are many function that may help you there, like:

使用Java ArrayUtils静态方法,有很多功能可以帮助你,比如:

Add functions:

static int[]    add(int[] array, int element) 
          Copies the given array and adds the given element at the end of the new array.
static int[]    add(int[] array, int index, int element) 
          Inserts the specified element at the specified position in the array.

Remove functions:

static int[]    remove(int[] array, int index) 
          Removes the element at the specified position from the specified array.

#2


0  

It looks like you're trying to simulate the action of an ArrayList! I'd recommend using an ArrayList to manage your list of arrays. If, however, you're only allowed to use an array, I'm afraid that unless you know how many maximum elements you're going to have in your outer array, you'll need to copy the way the ArrayList class works(with a few changes), which does indeed involve reallocating the array.

看起来你正试图模拟ArrayList的动作!我建议使用ArrayList来管理数组列表。但是,如果你只允许使用数组,我担心除非你知道外部数组中有多少个最大元素,否则你需要复制ArrayList类的工作方式(有一些变化),确实涉及重新分配数组。

However, have no fear because you can indeed reallocate the array without losing the contents of it. In the Arrays class, there's a static method called copyOf(). This allows you to make a new array of the size you want while retaining the contents of your old array.

但是,不要害怕,因为您确实可以重新分配数组而不会丢失它的内容。在Arrays类中,有一个名为copyOf()的静态方法。这允许您在保留旧数组的内容的同时创建所需大小的新数组。

Let's have an example:

我们举个例子:

boolean addLineSegment(int[] segment){
    if(segment is not valid)
        return false;

    lineSegments=Arrays.copyOf(lineSegments,lineSegments.length+1);
    lineSegments[lineSegments.length-1]=segment;
    return true;
}

This fulfills the requirement of increasing the size of the array by one while still retaining the old elements. For this to work, the array must start out with a size of zero, and it will then grow from then on.

这满足了将数组的大小增加一个同时仍然保留旧元素的要求。为此,数组必须以零大小开始,然后从那时开始增长。

This differs from the way the ArrayList class works in that while this one increases by one every time, the ArrayList class keeps track of the current index of the last element, and starts with an array of length 10, doubling every time the cap is reached. However, your requirements state that the size must increase by 1 each time so the solution I proposed should work fine.

这与ArrayList类的工作方式不同,虽然每次增加1,但ArrayList类会跟踪最后一个元素的当前索引,并以长度为10的数组开始,每次达到上限时加倍。但是,您的要求规定每次大小必须增加1,因此我提出的解决方案应该可以正常工作。

#1


4  

Use Java ArrayUtils static methods, there are many function that may help you there, like:

使用Java ArrayUtils静态方法,有很多功能可以帮助你,比如:

Add functions:

static int[]    add(int[] array, int element) 
          Copies the given array and adds the given element at the end of the new array.
static int[]    add(int[] array, int index, int element) 
          Inserts the specified element at the specified position in the array.

Remove functions:

static int[]    remove(int[] array, int index) 
          Removes the element at the specified position from the specified array.

#2


0  

It looks like you're trying to simulate the action of an ArrayList! I'd recommend using an ArrayList to manage your list of arrays. If, however, you're only allowed to use an array, I'm afraid that unless you know how many maximum elements you're going to have in your outer array, you'll need to copy the way the ArrayList class works(with a few changes), which does indeed involve reallocating the array.

看起来你正试图模拟ArrayList的动作!我建议使用ArrayList来管理数组列表。但是,如果你只允许使用数组,我担心除非你知道外部数组中有多少个最大元素,否则你需要复制ArrayList类的工作方式(有一些变化),确实涉及重新分配数组。

However, have no fear because you can indeed reallocate the array without losing the contents of it. In the Arrays class, there's a static method called copyOf(). This allows you to make a new array of the size you want while retaining the contents of your old array.

但是,不要害怕,因为您确实可以重新分配数组而不会丢失它的内容。在Arrays类中,有一个名为copyOf()的静态方法。这允许您在保留旧数组的内容的同时创建所需大小的新数组。

Let's have an example:

我们举个例子:

boolean addLineSegment(int[] segment){
    if(segment is not valid)
        return false;

    lineSegments=Arrays.copyOf(lineSegments,lineSegments.length+1);
    lineSegments[lineSegments.length-1]=segment;
    return true;
}

This fulfills the requirement of increasing the size of the array by one while still retaining the old elements. For this to work, the array must start out with a size of zero, and it will then grow from then on.

这满足了将数组的大小增加一个同时仍然保留旧元素的要求。为此,数组必须以零大小开始,然后从那时开始增长。

This differs from the way the ArrayList class works in that while this one increases by one every time, the ArrayList class keeps track of the current index of the last element, and starts with an array of length 10, doubling every time the cap is reached. However, your requirements state that the size must increase by 1 each time so the solution I proposed should work fine.

这与ArrayList类的工作方式不同,虽然每次增加1,但ArrayList类会跟踪最后一个元素的当前索引,并以长度为10的数组开始,每次达到上限时加倍。但是,您的要求规定每次大小必须增加1,因此我提出的解决方案应该可以正常工作。