arraylist

ArrayList containing different objects of the same superclass - how to access method of a subclass

夙愿已清 提交于 2019-12-19 17:58:03
问题 Hi I'm wondering if there is a simple solution to my problem, I have an ArrayList : ArrayList <Animal> animalList = new ArrayList<Animal>(); /* I add some objects from subclasses of Animal */ animalList.add(new Reptile()); animalList.add(new Bird()); animalList.add(new Amphibian()); They all implement a method move() - The Bird flies when move() is called. I know I can access common methods and properties of the super class by using this public void feed(Integer animalIndex) { Animal aAnimal

Is this java Object eligible for garbage collection in List

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 17:12:54
问题 What I am asking might be a stupid question so please pardon me for that. So it goes like this : List<Boss> bossList = new ArrayList<Boss>(); Boss b = null; for(Employee e : List<Employee> myList){ b = new Boss(); b.setEmployee(e); bossList.add(b); b = null; } So in above scenario, I am creating lot of Boss objects and then de-referencing them(I know I don't need to write "b = null", but i did it for clarity of my question). In normal scenario, I would have marked them to garbage collection,

Is this java Object eligible for garbage collection in List

倾然丶 夕夏残阳落幕 提交于 2019-12-19 17:12:11
问题 What I am asking might be a stupid question so please pardon me for that. So it goes like this : List<Boss> bossList = new ArrayList<Boss>(); Boss b = null; for(Employee e : List<Employee> myList){ b = new Boss(); b.setEmployee(e); bossList.add(b); b = null; } So in above scenario, I am creating lot of Boss objects and then de-referencing them(I know I don't need to write "b = null", but i did it for clarity of my question). In normal scenario, I would have marked them to garbage collection,

ArrayList集合的使用

回眸只為那壹抹淺笑 提交于 2019-12-19 14:52:01
数组的长度不可以改变 但是 ArrayList集合的长度是可以随意变化的 对于ArrayList来说,有一个尖括号< E > ,代表的是泛型 泛型:也就是装在集合中的所有元素,全都是【统一】的什么类型,这与数组相像,只能是一种类型 注意 :泛型只能是引用类型,不能是基本数据类型。 注意: 对于ArrayList集合来说,直接打印得到的不是地址值,而是内容 如果内容为空,那么就会得到空的中括号,[] //导包 import java . util . ArrayList ; public class Demo { public static void main ( String [ ] args ) { //创建一个ArrayList集合对象,名称为arr,里面装的全是String字符串数据类型 //从JDK1.7开始,右侧加括号里的内容可以不写,但是尖括号还是要写的 ArrayList < String > arr = new ArrayList < > ( ) ; //直接打印,得到的为空的[] System . out . println ( arr ) ; } } ArrayList中常见的方法有: public boolean add(E e):向集合中添加元素,参数的类型和泛型一致。E对 照泛型 注意:对于ArrayList集合来说,add添加动作一定成功

How can I convert a text file into a list of int arrays

元气小坏坏 提交于 2019-12-19 12:49:38
问题 I have a text file containing the following content: 0 0 1 0 3 0 0 1 1 3 0 0 1 2 3 0 0 3 0 1 0 0 0 1 2 1 1 0 0 1 0 3 0 0 1 1 3 0 0 1 2 3 0 0 3 0 1 0 0 1 2 3 0 0 3 0 1 There are no spaces between the rows but there is a space between the numbers. I want to read these integers from a txt file and save in a list of int arrays in C#. 回答1: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace FileToIntList { class Program { static void

How to merge mutiple array in a single array

痴心易碎 提交于 2019-12-19 11:58:18
问题 As i searched alot but didn't get perfect solution of merging array in one single array. These arrays are Dynamic (may be increase in future- would 50+). So for that we have to use count() or for loops to fetch and then merge. Here's my code which i'm trying to resolve over core level. Please anyone tell me how may receive all values in Single array. Array( [0] => Array ( [0] => 123 [1] => 108 [2] => 58 [3] => 23 ) [1] => Array ( [0] => 93 [1] => 94 [2] => 95 [3] => 172 [4] => 30 ) [2] =>

How do I loop thorough a 2D ArrayList in Java and fill it?

纵然是瞬间 提交于 2019-12-19 11:56:27
问题 I am trying to use 2D arrayLists in Java. I have the definition: ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>(); How can I loop through it and enter in numbers starting from 1? I know that I can access a specific index by using: myList.get(i).get(j) Which will get the value. But how do I add to the Matrix? Thanks 回答1: You can use a nested for loop. The i-loop loops through the outer ArrayList and the j-loop loops through each individual ArrayList contained by

JList and ArrayList update

十年热恋 提交于 2019-12-19 11:36:31
问题 I would like to have example on how to update a JList when I add or remove elements from a ArrayList. The ArrayList is part of Model class. The Model class is passed to the view (which is a JPanel containing several swing components, and the JList I want to update) via its constructor. The model class is also injected in a class that reads values received from the server. When i received data from the server I add some of them to my arrayList by doing model.getArrayList().add(data). When I

How to get key from ArrayList nested in JSON using Groovy and change its value

拟墨画扇 提交于 2019-12-19 11:34:42
问题 I need to be able to find the key quote.orderAttributes[0].attributeDetail.name and set its value to null or any other value I want. I only need to do this for the first element in any list so selecting [0] is fine. I want to be able to use a path such as 'quote.orderAttributes.attributeDetail.name'. But given the amount of time I've spent so far, please advise of any better approaches. Here is the Json: { "source": "source", "orderId": null, "Version": null, "quote": { "globalTransactionId":

ArrayList concurrent access

岁酱吖の 提交于 2019-12-19 10:16:49
问题 I am aware that ArrayList is not thread safe, but I'm unsure about the exact implications of this. In the case of ThreadA and ThreadB both using an ArrayList , which of these situations will cause issues and necessitate synchronization? Both threads simultaneously reading the same index ThreadA replacing an element which ThreadB is attempting to access simultaneously, assuming that you don't care whether or not ThreadB gets the old or the new element. 回答1: Both threads simultaneously reading