arraylist

java中数组以及集合

柔情痞子 提交于 2020-01-05 01:18:20
java中数组: 数组在Java里是一种特殊类型,有别于普通的“类的实例”的对象。但实际数组也是一种对象类型,int[]a = new int[5] a是在java栈中分配的引用变量,类型是int[] 数组类型,指向在堆里面地址连续的实际数组对象。 在内存中,数组存储在连续的区域内部,因为数组中每个元素的类型相同,则占用的内存大小也一致,所以在访问数组中的元素时可以直接根据数组在内存中的起始位置以及下标来计算元素的位置,因此数组的访问速度很高。数组必须要初始化才能使用,初始化之后JVM会自动分配默认值,引用变量默认值是null。 数组和集合的区别: 1》数组初始化之后大小固定,无法再改变,集合大小可以改变。 2》同一个数组只能存储同一种数据类型(基本类型/引用类型).集合不考虑泛型可以存储多种数据类型,集合是存储对象的,所以基本类型不能放入集合,可以使用基本类型的包装类型。 3》若程序时不知道究竟需要多少对象,需要在空间不足时自动扩增容量,则需要使用容器类库,array不适用。 数组和集合之间进行转化: toArray():将集合转化为数组。 Arrays.asList():将数组转化为集合. 集合的体系结构 List、Set、Map是这个集合体系中最主要的三个接口。 List和Set继承自Collection接口。 Map也属于集合系统,但和Collection接口不同。

how to Iterate through an arrayList of object and find duplicates in object properties? [closed]

≡放荡痞女 提交于 2020-01-04 18:40:06
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I have the following class: class A { ArrayList<String> s = new ArrayList<>(); double d = Double.MAX_VALUE; } I've an arrayList of

Is it more efficient to use a JSONArray or normal Array for storing/reading data?

无人久伴 提交于 2020-01-04 09:18:06
问题 I'm working with an app that connects to a PHP/MySQL server from which everything is returned in JSON format. For example, a list of users is returned as a JSONArray of JSONObject . Each object contains the individual user's information (name, location, phone number, etc). In working with information in this format is is more efficient to leave everything in JSON format and only extract items from the array/objects as needed? Or is it better to extract everything from the JSONArray and

HashMap vs. ArrayList insertion performance confusion

只谈情不闲聊 提交于 2020-01-04 09:14:11
问题 From my understanding a hashmap insertion is O(1) and for an arraylist the insertion is O(n) since for the hashmap the hashfunction computes the hashcode and index and inserts the entry and an array list does a comparison every time it enters a new element. 回答1: Firstly, an operation of complexity O(1) does not always take lesser time than an operation of complexity O(n). O(1) only means that the operation takes a constant time (which could be any value), regardless of the size of the input.

Uses unchecked or unsafe operations

淺唱寂寞╮ 提交于 2020-01-04 09:05:33
问题 I keep getting an error that says: Note: ABag.java uses unchecked or unsafe operations. I googled it and found this post, and made the changes that I thought would remove the error but I continue to get the error. Is there anything else I can do to stop getting this error message? public class ABag<Item> implements BagInterface<Item> { private ArrayList<Item> bag; //creates an empty bag public ABag(){ bag = new ArrayList<Item>(); } //creates an empty set with initial capacity public ABag (int

Connect multiple arraylist to one single array list

别说谁变了你拦得住时间么 提交于 2020-01-04 07:58:23
问题 I have an array list of a array list and I want to make a separate view of it that will filter certain things from it and store it somewhere for later usage. I am planing on using an array list of array list and was wondering if array list has a way to be connected together where the change in one reflects another ? Any thoughts ? Its like an excel sheet and I would like to filter out certain numbers and I will display another view of the same sheet without really changing the original sheet.

Displaying objects using foreach loop

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 07:52:11
问题 I am having a problem while retrieving data from an ArrayList and displaying them into textboxs. I am getting an error: Unable to cast object of type 'Lab_9.Book' to type 'Lab_9.Magazine' . I tried use 2 foreach loops but that seems to be out of the question. How could I avoid this problem? Problem occurs here: // Displaying all Book objects from pubs ArrayList. foreach (Book list in pubs) { bookNumber++; // Count books from the begining. // Displaying and formating the output // in

How do I pass arraylist of a custom object from one java class to another? [duplicate]

好久不见. 提交于 2020-01-04 06:54:10
问题 This question already has answers here : How to send an object from one Android Activity to another using Intents? (35 answers) Closed 3 years ago . I have an Arraylist of custom objects. I am trying to pass it from one java file to another. I tried the putExtra method and the Parcelable option; but I'm unable to understand Parcelable . Here's my custom object: public class AddValues implements Serializable{ public int id; String value; public AddValues(int id, String value) { this.id = id;

How do I pass arraylist of a custom object from one java class to another? [duplicate]

拜拜、爱过 提交于 2020-01-04 06:54:05
问题 This question already has answers here : How to send an object from one Android Activity to another using Intents? (35 answers) Closed 3 years ago . I have an Arraylist of custom objects. I am trying to pass it from one java file to another. I tried the putExtra method and the Parcelable option; but I'm unable to understand Parcelable . Here's my custom object: public class AddValues implements Serializable{ public int id; String value; public AddValues(int id, String value) { this.id = id;

Altering hashCode of object inside of HashSet / HashMap

半腔热情 提交于 2020-01-04 06:24:20
问题 I am relatively new to Java and am puzzled about the following thing: I usually add objects to an ArrayList before setting its content. I.e., List<Bla> list = new ArrayList<>(); Bla bla = new Bla(); list.add(bla); bla.setContent(); // content influences hashCode This approach works great. I am concerned whether this approach will give me trouble when used with HashSet s or HashMap s. The internal hash table get set at the time the object is added. What will happen if setContent() gets called