【问题标题】:Common elements of muiltiple arrays but not using Lists多个数组的公共元素但不使用列表
【发布时间】:2014-09-28 16:56:06
【问题描述】:

有没有办法在一个数组中返回 2 个或更多数组的公共元素?我知道列表下的一些方法可以做到这一点,但有没有办法只使用数组来做到这一点?顺便说一句,我自己创建了一个 get 和 length,因为我正在创建一个名为 OrderedIntList 的数组。

例如:

1,3,5

1,6,7,9,3

1,3,10,11

结果:1​​,3

我试过这个,它输出两个数组之间的公共元素,而不是全部。 我知道有问题,但我不知道如何让它像它想象的那样工作:(

//返回输入数组的公共元素

public static OrderedIntList common(OrderedIntList ... lists){ 

int[] list = new int[10];

for(int x = 1; x <= lists.length -1; x++){

  for(int q = 0; q < lists[0].length()-1; q++) {

    for(int z = 0; z < lists[x].length(); z++) {

      if (lists[0].get(q)==lists[x].get(z)){
         list[q] = lists[0].get(q);
                  }
              }
          }
      }

 OrderedIntList newlist = new OrderedIntList(list);     

 return newlist;
  }

【问题讨论】:

  • 你的问题很好,但你的代码看起来:(
  • 纯粹出于好奇 - 为什么要避免使用列表?
  • 您的自定义对象不会是数组索引的。您需要先公开底层数组,然后才能对其进行索引。
  • 把你的问题分解成更小的问题。创建一个方法boolean isIntContainedInList(int i, OrderedIntList list)。然后创建一个方法 boolean isIntContainedInAllTheLists(int i, OrderedIntList[] listArray),它使用前面的方法。这些应该是非常简单的方法,但是一旦有了它们,算法就会变得更容易实现。
  • @Ant Veeranna 可能是因为这是一个家庭作业 :)

标签: java arrays


【解决方案1】:

这可以是一个简单的算法来解决它...

    1) Instantiate an instance variable of type array called
    "commonElements" pointing to the elements of the first Array. At the
    beginning these are your common elements.

    2) Create a method call getCommonElements(int[] commonElements,
    int[] newList). This method manipulates the commonElements array to leave 
    it with only the common elements between the two. (p.s Use a temporary 
    array to achieve this if you find it easier)

    3) Iterate over all the arrays present in "lists" starting from the
    second array.

    4) call the method at point 2 for each array .

对你来说最困难的部分是实现一个给定 2 个数组找到共同元素的方法!

【讨论】:

  • @dj robles 如果您想表现出色,请记住,在有序数组中,您可以在对数时间内找到元素是否存在。在第 2 点实现方法时请记住这一点)
  • @djrobles 很高兴它对您有所帮助。
【解决方案2】:

你可以使用

org.apache.commons.collections.CollectionUtils.intersection(java.util.Collection a, java.util.Collection b)

获取两个列表的交集(两个列表中都有元素)

并将您的数组作为集合传递:java.util.Arrays.asList(Object[] a);

但是处理数组是乏味的,充其量是。您应该考虑一下为什么不想使用 Collection...

【讨论】:

    【解决方案3】:

    作为部分答案,您可能会按照您的方式完全重新实现 OrderedIntList,因为 ArrayList 和朋友已经通过 Collections 类进行了排序。

    import java.util.Collections;
    public class OrderedIntList extends ArrayList<Integer> {
      @override // to effect sorted inserts
      public void add(Integer i) {
        this.add(i);
        Collections.sort(this);
        // done.
      }
    }
    

    想要对纯数组执行此操作是一个不错的练习,但是您最好通过快速排序正确实现排序(如果没有获得一百万个结果,您无法通过网络搜索 Java 实现)或插入排序(同样可网络搜索),并遵循相同的方法。

    任何时候你将一个数字压入数组:

    1. 猜猜号码的去向(尽管这是可选的),
    2. 插入号码,
    3. 如果您知道自己的猜测不完美,请重新排列数组。

    【讨论】:

    • 列表无法自行排序。
    • 哎呀,忘了把它变成 Collections.sort
    • 我仍然不完全相信这是一个很好的答案。似乎他们想要做一个 N 路交叉点来查看公共元素。列表似乎不适合此数据类型。
    • 是的,这是一个公平的观点。我将添加一些文字来提及这并不能解决原始问题,但确实解决了他们试图实现的orderedintlist。 (这个问题有点二合一)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    相关资源
    最近更新 更多