arraylist

JAVA: Preventing Duplicate Entries to an ArrayList

与世无争的帅哥 提交于 2019-12-19 06:00:55
问题 I am trying to prevent duplicate entries from being added to an ArrayList as the list is being populated whilst reading through each line of a file. Each line of the file is in the format "node1 node2" (tab delimiter). A duplicate here could either be "node1 node2" or "node2 node1". Here is my code to try and perform this operation: while((line = bufferedReader.readLine()) != null) { String delimiter = "\t"; String[] tempnodelist; tempnodelist = line.split(delimiter); for (int i=0; i <=

how to create a string from string array or arraylist?

主宰稳场 提交于 2019-12-19 05:46:24
问题 how can i extract all the elements in a string [] or arraylist and combine all the words with proper formating(with a single space) between them and store in a array.. String[] a = {"Java", "is", "cool"}; Output: Java is cool. 回答1: Use a StringBuilder. String[] strings = {"Java", "is", "cool"}; StringBuilder builder = new StringBuilder(); for (String string : strings) { if (builder.length() > 0) { builder.append(" "); } builder.append(string); } String string = builder.toString(); System.out

What does it mean that Java arrays are homogeneous, but ArrayLists are not?

杀马特。学长 韩版系。学妹 提交于 2019-12-19 05:22:27
问题 If we have a Type[], we can only store Type or its subtypes in it. The same goes for ArrayList. So why is it said that one is homogeneous while the other is not? 回答1: Arrays have a runtime check on the type of the added element. That is, if a new element that is not of the same type is added, an ArrayStoreException is thrown at runtime. That's why they are considered as "homegeneous". This is not true for ArrayList s ( List s in general). Due to type erasure at runtime, it can practically

How to remove an empty list from a list (Java)

大憨熊 提交于 2019-12-19 05:21:32
问题 I have searched for this but it's in other languages like Python or R (?). I have lists inside a list and I would like to remove the empty list. For example: [ [abc,def], [ghi], [], [], [jkl, mno]] I would like: [ [abc,def], [ghi], [jkl, mno]] How do I remove empty list from a list? Thanks! 回答1: You could try this as well: list.removeIf(p -> p.isEmpty()); 回答2: You could use: list.removeAll(Collections.singleton(new ArrayList<>())); 回答3: list.removeAll(Collections.singleton(new ArrayList<>()))

Why does Java ArrayList use per-element casting instead of per-array casting?

泄露秘密 提交于 2019-12-19 05:06:27
问题 What happens inside Java's ArrayList<T> (and probably many other classes) is that there is an internal Object[] array = new Object[n]; , to which T Objects are written. Whenever an element is read from it, a cast return (T) array[i]; is done. So, a cast on every single read. I wonder why this is done. To me, it seems like they're just doing unnecessary casts. Wouldn't it be more logical and also slightly faster to just create a T[] array = (T[]) new Object[n]; and then just return array[i];

Sort two arrayLists concurrently

[亡魂溺海] 提交于 2019-12-19 04:48:12
问题 Say I have two ArrayLists: name: [Four, Three, One, Two] num: [4, 3, 1, 2] If I do: Arrays.sort(num), then I have: name: [Four, Three, One, Two] num: [1, 2, 3, 4] Is there any way I can possibly do a sort on num and have it reflected in name as well, so that I might end up with: name: [One, Two, Three, Four] num: [1, 2, 3, 4] ? Please do help me out. I thought of Comparators and Objects, but barely know them at all. 回答1: You should somehow associate name and num fields into one class and then

How to parse JSON object from a website into an arraylist in android

北城以北 提交于 2019-12-19 04:45:10
问题 How can I parse a JSON object from a weblink into Android and store the different values into ArrayLists? The JSON object of users looks like the below. It comes from a website. {"Users":[{"name":"Kane","lon":"4.371645","lat":"31.396911"}, {"name":"Sam","lon":"4.129737","lat":"31.194824"}, {"name":"Ryan","lon":"4.023134","lat":"31.298222"}, {"name":"Jerry","lon":"4.262276","lat":"31.295054"}]} I want to parse in into Android and store the different values like name, lon and lat into an array

Java ArrayList.add() method thread safe for purely parallel adding? [duplicate]

我的未来我决定 提交于 2019-12-19 04:24:15
问题 This question already has answers here : Collections.synchronizedList and synchronized (6 answers) Closed 5 years ago . Consider a for-loop over a function that takes an ArrayList reference and adds an object to that ArrayList. I would now like to execute each function call in parallel. Is the ArrayList.add() method thread safe if I don't care about the sequence the objects are added and no function reads or manipulates any ArrayList elements? So I only want to make sure that at the end of

Reading text file into arraylist

岁酱吖の 提交于 2019-12-19 04:02:00
问题 I really new to Java so I'm having some trouble figuring this out. So basically I have a text file that looks like this: 1:John:false 2:Bob:false 3:Audrey:false How can I create an ArrayList from the text file for each line? 回答1: Read from a file and add each line into arrayList. See the code for example. public static void main(String[] args) { ArrayList<String> arr = new ArrayList<String>(); try (BufferedReader br = new BufferedReader(new FileReader(<your_file_path>))) { String sCurrentLine

Why does this not compile : List<List<String>> lss = new ArrayList<ArrayList<String>>(); [duplicate]

久未见 提交于 2019-12-19 03:39:05
问题 This question already has answers here : Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic? (17 answers) Closed 5 years ago . The below code : List<List<String>> lss = new ArrayList<ArrayList<String>>(); causes this compile time error : Type mismatch: cannot convert from ArrayList<ArrayList<String>> to List<List<String>> to fix I change the code to : List<ArrayList<String>> lss = new ArrayList<ArrayList<String>>(); Why is this error being thrown ? Is