arraylist

How to verify if a value in HashMap exist

丶灬走出姿态 提交于 2019-12-18 19:01:25
问题 I have the following HashMap where the key is a String and the value is represented by an ArrayList : HashMap<String, ArrayList<String>> productsMap = AsyncUpload.getFoodMap(); I also have another ArrayList<String> foods implemented in my application. My question is, What would be the best way to find out if my HashMap contains a Specific String from my second ArrayList ? I have tried without success: Iterator<String> keySetIterator = productsMap.keySet().iterator(); Iterator<ArrayList<String

Java深拷贝浅拷贝

青春壹個敷衍的年華 提交于 2019-12-18 18:46:51
首先, Java 中常用的拷贝操作有三个, operator = 、拷贝构造函数 和 clone() 方法。由于 Java 不支持运算符重载,我们无法在自己的自定义类型中定义 operator= 。拷贝构造函数大家应该很熟悉,现在看一下如何支持 clone 方法: 实现 Cloneable 接口,因为 Object 的 clone 方法将检查类是否实现了 Cloneable 接口,如果没有将抛出异常 CloneNotSupportedException 对象。 Cloneable 接口没有任何方法,只是个标志,所以只需要简单的写上 implements Cloneable 即可。 改写从 Object 继承而来的 clone 方法,使它的访问权限为 public ,因为为了防止意外的支持 clone 操作, Object 的 clone 方法是 protected 权限。 通过上面的分析,可以看出,如果我们要给自己的类添加拷贝功能,我们可以添加拷贝构造函数和实现 Cloneable 接口。 现在,来看一下不同的类型在拷贝过程中的表现: Operator = 拷贝构造函数 clone 方法 预定义非集合类型 深拷贝 如果支持拷贝构造函数的类型,则是深拷贝 不支持 自定义类型 浅拷贝 取决于实现 取决于实现 预定义集合类型 浅拷贝 会逐个调用每个元素的 operator= 方法

迭代器

﹥>﹥吖頭↗ 提交于 2019-12-18 18:42:14
1.1. 迭代器 为了方便的处理集合中的元素 ,Java中出现了一个对象,该对象提供了一些方法专门处理集合中的元素.例如删除和获取集合中的元素.该对象就叫做迭代器(Iterator). 对 Collection 进行迭代的类,称其为迭代器。还是面向对象的思想,专业对象做专业的事情,迭代器就是专门 取出 集合元素的对象。但是该对象比较特殊,不能直接创建对象(通过 new),该对象是以内部类的形式存在于每个集合类的内部。 如何获取迭代器? Collection接口中定义了获取集合类迭代器的方法(iterator()),所以所有的Collection体系集合都可以获取自身的迭代器。 正是由于 每一个容器都有取出 元素的 功能。这些功能定义都一样,只不过实现的具体方式不同 ( 因为每一个容器的数据结构不一样 ) 所以 对共性的取出功能 进行了抽取,从而出现了 Iterator 接口。而每一个容器都在其内部对该接口进行了内部类的实现。也就是将取出方式的细节进行封装。 1.1.1. Iterable Jdk1.5 之后添加的新接口 , Collection 的父接口 . 实现了 Iterable 的类就是可迭代的 .并且 支持增强 for 循环 。该接口只有一个方法即获取迭代器的方法iterator ()可以获取每个容器自身的迭代器 Iterator 。( Collection

结构型模式(一) 适配器模式(Adapter)

妖精的绣舞 提交于 2019-12-18 18:35:25
一、动机(Motivation) 在软件系统中,由于应用环境的变化,常常需要将“一些现存的对象”放在新的环境中应用,但是新环境要求的接口是这些现存对象所不满足的。 如何应对这种“迁移的变化”?如何既能利用现有对象的良好实现,同时又能满足新的应用环境所要求的接口? 二、意图(Intent) 将一个类的接口转换成客户希望的另一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 例说Adapter应用 这种实际上是一种委派的调用,本来是发送请求给MyStack,但是MyStack实际上是委派给list去处理。MyStack在这里其实就是Adapter(适配对象),list即是Adaptee(被适配的对象),而IStack就是客户期望的接口。太直接了,没什么可说的。 三、结构(Structure) 适配器有两种结构 1、对象适配器(更常用) 对象适配器使用的是对象组合的方案,它的Adapter和Adaptee的关系是组合关系,即上面例子中MyStack和list是组合关系。 OO中优先使用组合模式,组合模式不适用再考虑继承。因为组合模式更加松耦合,而继承是紧耦合的,父类的任何改动都要导致子类的改动。 上面的例子就是对象适配器。 2、类适配器(不推荐使用) 下面的例子是类适配器。 Adapter继承了ArrayList,也继承了IStack接口

Get index of an arraylist using property of an contained object in java [duplicate]

若如初见. 提交于 2019-12-18 16:34:32
问题 This question already has answers here : Searching in a ArrayList with custom objects for certain strings (9 answers) Closed 5 years ago . I'm having an list of Object type. In that I have one String property idNum. Now I want to get the index of the object in the list by passing the idNum. List<Object1> objList=new ArrayList<Object1>(); I don't know how to give objList.indexOf(// Don't know how to give here); Is it possible to do this without iterating the list. I want to use indexOf()

Getting Index of an item in an arraylist;

白昼怎懂夜的黑 提交于 2019-12-18 14:12:12
问题 I have a Class called AuctionItem . The AuctionItem Class has a method called getName() that returns a String . If I have an ArrayList of type AuctionItem , what is the best way to return the index of an item in the ArrayList that has a specific name? I know that there is an .indexOf() function. The parameter for this function is an object. To find the item that has a name, should I just use a for loop, and when the item is found, return the element position in the ArrayList ? Is there a

When you make an ArrayList without specifying the object type, does it create it automatically when you add the first object?

 ̄綄美尐妖づ 提交于 2019-12-18 14:05:26
问题 For example, instead of doing ArrayList<ClassName> variableName; you do ArrayList variableName; then later you add an object of type "ClassName" variableName.add(objectName); will that automatically set the type of your array as ArrayList<ClassName> ? 回答1: No. Generics are for compile time only. You are just losing the benefit of that check. At runtime all generic information is erased In other words, ArrayList<Type> at runtime is just an ArrayList. The benefit of doing that over just a List

Passing ArrayList of objects through intent - Java (Android)

﹥>﹥吖頭↗ 提交于 2019-12-18 13:02:00
问题 I am trying to pass an ArrayList of objects through an intent but cannot get it to work. Here is what I have: public class QuestionView extends Activity { //variables and other methods... public void endQuiz() { Intent intent = new Intent(QuestionView.this, Results.class); intent.putExtra("correctAnswers", correctAnswers); intent.putExtra("wrongAnswers", wrongAnswers); intent.putParcelableArrayListExtra("queries", queries); startActivity(intent); } } Intents are being received here: public

Passing ArrayList of objects through intent - Java (Android)

﹥>﹥吖頭↗ 提交于 2019-12-18 13:01:06
问题 I am trying to pass an ArrayList of objects through an intent but cannot get it to work. Here is what I have: public class QuestionView extends Activity { //variables and other methods... public void endQuiz() { Intent intent = new Intent(QuestionView.this, Results.class); intent.putExtra("correctAnswers", correctAnswers); intent.putExtra("wrongAnswers", wrongAnswers); intent.putParcelableArrayListExtra("queries", queries); startActivity(intent); } } Intents are being received here: public

Which one runs faster, ArrayList or LinkedList? [duplicate]

流过昼夜 提交于 2019-12-18 12:55:30
问题 This question already has answers here : When to use LinkedList over ArrayList in Java? (32 answers) Performance differences between ArrayList and LinkedList (11 answers) Closed 6 years ago . List li = new LinkedList(); for (int i = 0; i < 100; i++) { li.add(i); } long start1 = System.nanoTime(); li.get(57); long end1 = System.nanoTime(); long diff1 = end1-start1; System.out.println("Time taken by LinkedList = "+diff1); List al = new ArrayList(); for (int i = 0; i < 100; i++) { al.add(i); }