热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用含有整数数组的ArrayList-UsingcontainsonanArrayListwithintegerarrays

IhaveanArrayList<int[]>,andIaddanarraytoit.我有一个ArrayList,我给它添加了一个数组。ArrayL

I have an ArrayList, and I add an array to it.

我有一个ArrayList ,我给它添加了一个数组。

ArrayList j = new ArrayList();
int[] w = {1,2};
j.add(w);

Suppose I want to know if j contains an array that has {1,2} in it without using w, since I will be calling it from another class. So, I create a new array with {1,2} in it...

假设我想知道j是否包含一个包含{1,2}且不使用w的数组,因为我将从另一个类调用它。因此,我创建了一个包含{1,2}的新数组…

int[] t = {1,2};
return j.contains(t);

...but this would return false even though w was added to the list, and w contains the exact same array as t.

…但这将返回false,尽管w被添加到列表中,而w包含与t相同的数组。

Is there a way to use contains such that I can just check to see if one of the elements of the ArrayList has the array value {1,2}?

是否有一种方法可以使用contains,我可以检查ArrayList的一个元素是否具有数组值{1,2}?

7 个解决方案

#1


7  

Arrays can only be compared with Arrays.equals().

数组只能与Arrays.equals()相比较。

You probably want an ArrayList of ArrayLists.

你可能想要一个数组列表。

ArrayList> j = new ArrayList>();
ArrayList w = new ArrayList();
w.add(1); w.add(2);
j.add(w);
ArrayList t = new ArrayList();
t.add(1); t.add(2);
return j.contains(t); // should return true.

#2


7  

The problem here is that arrays don't override Object.equals(Object), So the comparison between two list entries happens with the default equals() implementation

这里的问题是数组不覆盖Object.equals(Object),因此在默认的equals()实现中,两个列表条目之间的比较发生

// from Object.class
public boolean equals(Object obj) {
return (this == obj);
}

So you have to iterate over the list and check all entries using Arrays.equals(int[], int[]). Here's a Helper method that does this:

因此,必须遍历列表并使用数组检查所有条目。=(int[],int[])。这里有一个帮助方法可以做到这一点:

public static boolean isInList(
    final List list, final int[] candidate){

    for(final int[] item : list){
        if(Arrays.equals(item, candidate)){
            return true;
        }
    }
    return false;
}

Update: Ever since Java 8, this has got a lot simpler:

更新:自从Java 8以来,这变得简单多了:

public static boolean isInList(
        final List list, final int[] candidate) {

    return list.stream().anyMatch(a -> Arrays.equals(a, candidate));
            //  ^-- or you may want to use .parallelStream() here instead
}

#3


0  

You need to iterate through the list and manually check whether an array matches your criteria.

您需要遍历列表并手动检查数组是否匹配您的条件。

public static boolean containsSubArray(List j, int[] sub) {
   for ( int[] arr : j ) {
      if (arr contains elements of sub) {
         return true;
      }
   }
   return false;
}

If you want an exact match, you can make use of Arrays.equals(). I don't think there's a library function to do a contains all on an array though, so you would have to write that yourself if that's what you wanted.

如果你想要精确的匹配,你可以使用Arrays.equals()。我不认为有一个库函数可以在数组中包含所有的元素,所以如果这是你想要的,你必须自己写出来。

#4


0  

"contains" contract checks for equality. So in your case what is failing is equality of int[]. Since Array does not override the equals method from Object you will need a workaround to check for containment.

包含“平等合同检查”。在你的例子中,失败的是int[]的平等。由于数组不覆盖来自对象的equals方法,所以您将需要一个变通方法来检查包含。

If you need to check for containment within the Array then you are left with no choice but to iterate through the ArrayList and do the comparison yourself.

如果您需要检查数组中的容器,那么您将别无选择,只能遍历ArrayList并自己进行比较。

#5


0  

from java api:

从java api:

public boolean contains(Object o)

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

如果此列表包含指定的元素,则返回true。更正式地说,如果且仅当此列表包含至少一个元素e (o= null ?)e = = null:o.equals(e))。

since int[] is a primitive, im pretty sure no .equals method exists so its my guess it would always return false.

由于int[]是一个原语,我很确定不存在.equals方法,所以我猜想它总是返回false。

I would recommend a different way of storing the data? maybe with a key of some sort?

我建议用一种不同的方式来存储数据?也许用什么钥匙?

#6


0  

Two java array arrays are equal iff they have the same object reference. Content doesn't matter.

两个java数组是相等的iff,它们具有相同的对象引用。内容不重要。

You're looking for a way to check if they have an equal content. This could help:

你正在寻找一种方法来检查它们是否有相同的内容。这可以帮助:

 Arrays.equals(new int[]{1,2}, new int[]{1,2});   // evaluates to true
 Arrays.equals(new int[]{1,2}, new int[]{2,1});   // evaluates to false (!)

If order shouldn't affect equality, then you will have to implement a static equals method by yourself.

如果顺序不应该影响等式,那么您将不得不自己实现一个静态等号方法。

#7


0  

First they are not the same Object reference, so they are not equal. equals() will return false. For your condition, you will need to implement a method to compare them yourself.

首先它们不是相同的对象引用,所以它们不相等。equals()将返回false。对于您的条件,您将需要实现一个方法来自己比较它们。


推荐阅读
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 集合的遍历方式及其局限性
    本文介绍了Java中集合的遍历方式,重点介绍了for-each语句的用法和优势。同时指出了for-each语句无法引用数组或集合的索引的局限性。通过示例代码展示了for-each语句的使用方法,并提供了改写为for语句版本的方法。 ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 本文介绍了Python函数的定义与调用的方法,以及函数的作用,包括增强代码的可读性和重用性。文章详细解释了函数的定义与调用的语法和规则,以及函数的参数和返回值的用法。同时,还介绍了函数返回值的多种情况和多个值的返回方式。通过学习本文,读者可以更好地理解和使用Python函数,提高代码的可读性和重用性。 ... [详细]
  • STL迭代器的种类及其功能介绍
    本文介绍了标准模板库(STL)定义的五种迭代器的种类和功能。通过图表展示了这几种迭代器之间的关系,并详细描述了各个迭代器的功能和使用方法。其中,输入迭代器用于从容器中读取元素,输出迭代器用于向容器中写入元素,正向迭代器是输入迭代器和输出迭代器的组合。本文的目的是帮助读者更好地理解STL迭代器的使用方法和特点。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
author-avatar
可爱鼠标1985
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有