arraylist

Java , Removing object from ArrayList

五迷三道 提交于 2019-12-30 16:26:06
问题 I have ClassA which has a static ArrayList of Objects public static ArrayList<Meteorit> meteorits = new ArrayList<Meteorit>(); Now I want to remove an object from this list like this ClassA.meteorits.remove(this); This is written in Meteorit class . But it throws exception when I want to use the objects in the ArrayList . Exception in thread "LWJGL Application" java.util.ConcurrentModificationException I used Iterator to remove objects from ArrayList but now I dont have an idea how to use it

Java集合详解8:Java集合类细节精讲,细节决定成败

孤街浪徒 提交于 2019-12-30 14:02:04
《Java集合详解系列》是我在完成夯实Java基础篇的系列博客后准备开始写的新系列。 这些文章将整理到我在GitHub上的《Java面试指南》仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下Star、fork哈 文章首发于我的个人博客: www.how2playlife.com 今天我们来探索一下Java集合类中的一些技术细节。主要是对一些比较容易被遗漏和误解的知识点做一些讲解和补充。可能不全面,还请谅解。 初始容量 集合是我们在Java编程中使用非常广泛的,它就像大海,海纳百川,像万能容器,盛装万物,而且这个大海,万能容器还可以无限变大(如果条件允许)。当这个海、容器的量变得非常大的时候,它的初始容量就会显得很重要了,因为挖海、扩容是需要消耗大量的人力物力财力的。 同样的道理,Collection的初始容量也显得异常重要。所以:对于已知的情景,请为集合指定初始容量。 public static void main(String[] args) { StudentVO student = null; long begin1 = System.currentTimeMillis(); List<StudentVO> list1 = new ArrayList<>(); for(int i = 0 ; i

《程序员面试宝典》解题报告2-牛客

有些话、适合烂在心里 提交于 2019-12-30 13:11:37
比较经典的一些题:31、32、37、38、40、41、44、45、46、47、48、49、50、51、52、54、56、57、58、59、60 31、找出缺失的整数 题目描述 数组A包含了0到n的所有整数,但其中缺失了一个。对于这个问题,我们设定限制,使得一次操作无法取得数组number里某个整数的完整内容。唯一的可用操作是询问数组中第i个元素的二进制的第j位(最低位为第0位),该操作的时间复杂度为常数,请设计算法,在O(n)的时间内找到这个数。 给定一个数组number,即所有剩下的数按从小到大排列的二进制各位的值,如A[0][1]表示剩下的第二个数二进制从低到高的第二位。同时给定一个int n,意义如题。请返回缺失的数。 测试样例: [[0],[0,1]] 返回:1 import java . util . * ; /* 位运算的一种思路: 另外的一道题,找出不重复的数字 任意一个数字与本身异或都是0 把0~n都异或一次,然后再与数组中的数异或,结果就是缺失的整数 但是现在表示成了二进制,所以不能用。 已经排好序,所以偶数奇数交替,下标奇偶与数字奇偶一样,缺失的那个数就是不一样的那个下标值。 还有一种解法是数学方法。 * */ public class Finder { public int findMissing ( int [ ] [ ] numbers , int n )

打开BeanFactory ignoreDependencyInterface方法的正确姿势

拜拜、爱过 提交于 2019-12-30 11:44:35
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在阅读Spring容器扩展部分源码的过程中,我了解到BeanFactory接口中有个方法叫ignoreDependencyInterface。从官方文档的“字面”来看,其作用指定自动装配(autowiring)的时候忽略的接口。还有一个很相似的方法叫ignoreDependencyType,同样其官方字面意思是指自动装配(autowiring)的时候忽略的类。 究竟这两个方法是不是我们的理解相同呢?真的可以让指定的接口和类在自动装配的时候被忽略?有没有注意不到的坑? /** * Ignore the given dependency interface for autowiring. * <p>This will typically be used by application contexts to register * dependencies that are resolved in other ways, like BeanFactory through * BeanFactoryAware or ApplicationContext through ApplicationContextAware. * <p>By default, only the BeanFactoryAware interface

sorting string arraylist in android [duplicate]

霸气de小男生 提交于 2019-12-30 11:23:35
问题 This question already has answers here : Sorting arraylist in alphabetical order (case insensitive) (7 answers) Closed 4 years ago . I have a string arraylist called names . How do I sort the arraylist in alphabetical order? 回答1: ArrayList<String> names = new ArrayList<String>(); names =fillNames() // whatever method you need to fill here; Collections.sort(names); http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29 Sorts the specified list into

sorting string arraylist in android [duplicate]

孤者浪人 提交于 2019-12-30 11:22:19
问题 This question already has answers here : Sorting arraylist in alphabetical order (case insensitive) (7 answers) Closed 4 years ago . I have a string arraylist called names . How do I sort the arraylist in alphabetical order? 回答1: ArrayList<String> names = new ArrayList<String>(); names =fillNames() // whatever method you need to fill here; Collections.sort(names); http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29 Sorts the specified list into

How to sort an ArrayList with object using stream().sorted()

断了今生、忘了曾经 提交于 2019-12-30 09:56:47
问题 I am having real trouble with using stream and sorted to sort my ArrayList and hope someone can help out. The code uses Croatian words, but I don't think that will be a problem for someone who understands what I mean. This is the ArrayList ArrayList<Publikacija> listaPublikacija = new ArrayList<>(); listaPublikacija.add(prvaKnjiga); listaPublikacija.add(drugaKnjiga); listaPublikacija.add(prviCasopis); listaPublikacija.add(drugiCasopis); In my assignment I am supposed to sort these objects

How do I enter parameters for an ArrayList in BlueJ?

爱⌒轻易说出口 提交于 2019-12-30 09:36:12
问题 In BlueJ, if I write a method that takes an array as a parameter, then when I want to test that method with a method call I have to enter the elements with curly braces, so: {1,2,3} How do I do a method call for an ArrayList ? Here is my code: import java.util.*; public class Test2{ public static int[] toArray(ArrayList<Integer>a){ int len = a.size(); int []b = new int[len]; for(int i = 0; i<len; i++){ b[i] = a.get(i); } return b; } } Now I want to test it in BlueJ, what should I type in the

How to read the txt file data into arraylist

放肆的年华 提交于 2019-12-30 07:34:09
问题 This following data is inside the Admin.txt file id, username, password, name, email, contactNumber, icNumber AD001|admin|admin|admin|admin@gmail.com|0123456789|931245678976 AD002|jeff|jeff|jefferyleo|jefferyleo@gmail.com|12345678790|941457431246 how should I read these data into an arraylist?? List<Admin> adminList = new ArrayList<Admin>(); This is the arrayList that I decalred. BufferedReader br = new BufferedReader(new FileReader("Admin.txt")); String strLine; while((strLine = br.readLine(

Reading integers from file into an arraylist

与世无争的帅哥 提交于 2019-12-30 07:09:53
问题 import java.util.Scanner; import java.io.*; import java.util.ArrayList; public class Test { public static void main (String args[]) throws java.io.IOException { Scanner s = new Scanner(new File("filepath")); ArrayList<Integer> list = new ArrayList<Integer>(); while (s.hasNext()){ if(s.hasNextInt()){ list.add(s.nextInt()); } } s.close(); System.out.println(list); } } I'm trying to read only integers from a text file that has the following format per line: text integer text. This is what I have