从数组中删除点并返回新数组-Java [英] Removing Points from an Array and returning a new Array - Java

查看:143
本文介绍了从数组中删除点并返回新数组-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个名为removeSomePoints的方法.此方法需要一个数组 点,并返回与点相同的新点数组 原始数组,除了它删除所有具有x-和 y坐标相差小于20.换句话说, 返回的数组的大小可能会比原始数组小 大批.此方法应保持原始数组不变.到 阐明:如果以下任何点在原始数组中, 它们将不在返回的数组中.

Write a method named removeSomePoints. This method takes an array of Points, and returns a new array of points that is the same as the original array, except that it removes all points that have x- and y-coordinates that differ by less than 20. In other words, the returned array will probably have a smaller size than the original array. This method should leave the original array untouched. To clarify: If any of the following points were in the original array, they would not be in the array that is returned.

100 90(x和y相差10)

100 90 (x and y differ by 10)

90100(x和y相差10)

90 100 (x and y differ by 10)

3 22(x和y相差19)

3 22 (x and y differ by 19)

另一方面,以下任何点将在数组中 返回:

On the other hand, any of the following points would be in the array that is returned:

100 80(x和y相差20)

100 80 (x and y differ by 20)

80 100(x和y相差20)

80 100 (x and y differ by 20)

2 25(x和y相差23)

2 25 (x and y differ by 23)

我的代码:

   public static Point[] removeSomePoints(Point[] arr) 
   {

    int count = 0;      
    for (int i = 0; i < arr.length; i++) {
         if (Math.abs(arr[i].getX() - arr[i].getY()) > 19) {
            count++;
          }
         }

        Point[] finalArr = new Point[count];

        for (int i = 0; i < finalArr.length; i++) {
          if (Math.abs(arr[i].getX() - arr[i].getY()) > 19) {
               finalArr[i] = arr[i];
          }

     }

       return finalArr;
    }

似乎无法找出问题所在.它正在返回一个数组,但是它什么都不会改变.

Can't seem to figure out what is going wrong. It is returning an array but it doesn't change anything.

推荐答案

好,因此您的程序当前无法正常工作,因为您确定了长度为N). >.现在,确保已保证N小于M,请注意,然后遍历输入数组(长度为N)的 子集 并将该子集中的所有匹配项复制到您的输出数组中.这根本是不正确的.

Ok, so your program is currently not working because you determine the number of matching items (lets say N), in your input array of length M. Now given that N is guaranteed to be less than M, notice that you are then iterating over a subset of your input array (of length N) and copying any matches in that subset into your output array. This is simply incorrect.

我认为这是一个学习项目-它的课程重点显然是为代码选择合适的数据结构.数组在处理固定大小的集合时非常有用,但不适用于可变大小的集合.对于可变数据,列表绝对是可行的方法.

I take it this is a learning project - and its obvious the key point of the lesson is in picking appropriate data structures for your code. Arrays are great when dealing with fixed size sets, but not so appropriate for variable sized sets. For variable data, lists are definitely the way to go.

所以让我们看一些伪代码:

So let's take a look at some pseudocode:

public static Point[] removeSomePoints(Point[] arr) {
    // If 'arr' is null, return null
    // If 'arr' is empty, return empty

    // Initialize a variable list structure, of at least arr.length in size
    // For each element in arr
    //     if element matches condition, add to variable list
    // End for
    // 
    // Convert variable list into array and return
}

简而言之,这就是您的算法.将其转换为真实代码:

Thats your algorithm, in a nutshell. Converting that into real code:

public static Point[] removeSomePoints(Point[] arr) {
    if(arr == null) return null;
    if(arr.length == 0) return new Point[0];

    List<Point> outputList = new ArrayList<Point>(arr.length);
    for(Point p : arr) {
        if (Math.abs(p.getX() - p.getY()) > 19) outputList.add(p);
    }

    return outputList.toArray(new Point[outputList.size());
 }

请不要忘记阅读的文档列表 ArrayList ,在为了更好地了解它们的工作原理.

Don't forget to go over the documentation for List and ArrayList, in order to better understand how they work.

这篇关于从数组中删除点并返回新数组-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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