arraylist

How do I update the element at a certain position in an ArrayList? [duplicate]

你。 提交于 2019-12-17 07:14:33
问题 This question already has answers here : Java ArrayList replace at specific index (5 answers) Closed last year . I have one ArrayList of 10 String s. How do I update the index 5 with another String value? 回答1: Let arrList be the ArrayList and newValue the new String , then just do: arrList.set(5, newValue); This can be found in the java api reference here. 回答2: list.set(5,"newString"); Reference 回答3: arrList.set(5,newValue); and if u want to update it then add this line also youradapater

How to insert an object in an ArrayList at a specific position

假装没事ソ 提交于 2019-12-17 07:12:33
问题 Suppose I have an ArrayList of objects of size n. Now I want to insert an another object at specific position, let's say at index position k (is greater than 0 and less than n) and I want other objects at and after index position k to shift one index position ahead. So is there any way to do this directly in Java. Actually i want to keep the list sorted while adding new object. 回答1: To insert value into ArrayList at particular index, use: public void add(int index, E element) This method will

How to insert an object in an ArrayList at a specific position

廉价感情. 提交于 2019-12-17 07:12:31
问题 Suppose I have an ArrayList of objects of size n. Now I want to insert an another object at specific position, let's say at index position k (is greater than 0 and less than n) and I want other objects at and after index position k to shift one index position ahead. So is there any way to do this directly in Java. Actually i want to keep the list sorted while adding new object. 回答1: To insert value into ArrayList at particular index, use: public void add(int index, E element) This method will

How to find the length of an array list? [duplicate]

我只是一个虾纸丫 提交于 2019-12-17 07:04:10
问题 This question already has answers here : How can I get the size of an array, a Collection, or a String in Java? (2 answers) Closed 5 years ago . I don't know how to find the length of an array list. I think you might need to use blank.length(); but I'm not sure. I made an array list ArrayList<String> myList = new ArrayList<String>(); but how do I write code to print out the size of myList? 回答1: The size member function. myList.size(); http://docs.oracle.com/javase/6/docs/api/java/util

How to use invokeAll() to let all thread pool do their task?

谁都会走 提交于 2019-12-17 06:36:07
问题 ExecutorService pool=Executors.newFixedThreadPool(7); List<Future<Hotel>> future=new ArrayList<Future<Hotel>>(); List<Callable<Hotel>> callList = new ArrayList<Callable<Hotel>>(); for(int i=0;i<=diff;i++){ String str="2013-"+(liDates.get(i).get(Calendar.MONTH)+1)+"-"+liDates.get(i).get(Calendar.DATE); callList.add(new HotelCheapestFare(str)); } future=pool.invokeAll(callList); for(int i=0;i<=future.size();i++){ System.out.println("name is:"+future.get(i).get().getName()); } Now I want pool to

How do I clone a generic List in Java?

眉间皱痕 提交于 2019-12-17 06:27:35
问题 I have an ArrayList<String> that I'd like to return a copy of. ArrayList has a clone method which has the following signature: public Object clone() After I call this method, how do I cast the returned Object back to ArrayList<String> ? 回答1: ArrayList newArrayList = (ArrayList) oldArrayList.clone(); 回答2: Why would you want to clone? Creating a new list usually makes more sense. List<String> strs; ... List<String> newStrs = new ArrayList<>(strs); Job done. 回答3: This is the code I use for that:

What is the Simplest Way to Reverse an ArrayList?

时光怂恿深爱的人放手 提交于 2019-12-17 06:22:04
问题 What is the simplest way to reverse this ArrayList? ArrayList<Integer> aList = new ArrayList<>(); //Add elements to ArrayList object aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"); aList.add("5"); while (aList.listIterator().hasPrevious()) Log.d("reverse", "" + aList.listIterator().previous()); 回答1: Collections.reverse(aList); Example (Reference): ArrayList aList = new ArrayList(); //Add elements to ArrayList object aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"

面经——Java基础

﹥>﹥吖頭↗ 提交于 2019-12-17 06:15:05
Java基础 ArrayList 和 LinkedList 区别 双亲委派模型以及优点 String是否可以被继承及相关原因 String 和 StringBuffer、StringBuilder 的区别是什么?String 为什么是不可变的? 接口和抽象类的区别 Java 中的异常体系 synchronized 底层实现 final关键字 重载和重写的区别 浅拷贝和深拷贝的区别 static 关键字 wait和sleep区别 反射 为什么java是跨平台的 int和Integer区别 HashMap详解 HashSet 和 HashMap 区别 HashMap 和 HashTable 区别 ConcurrentHashMap解析 ConcurrentHashMap 和 Hashtable 的区别 == 和 equals 区别 Java 序列化和反序列化 注:题目从牛客 Java部门面经整理而来。 2020秋招面经大汇总!(岗位划分) ArrayList 和 LinkedList 区别 1. ArrayList概览 因为 ArrayList 是基于数组实现的,所以支持快速随机访问。RandomAccess 接口标识着该类支持快速随机访问。 public class ArrayList < E > extends AbstractList < E > implements List

Pass ArrayList<? implements Parcelable> to Activity

百般思念 提交于 2019-12-17 05:55:10
问题 I have searched a few topics but not found a solution to my problem. public class Series implements Parcelable { private String name; private int numOfSeason; private int numOfEpisode; /** Constructors and Getters/Setters have been removed to make reading easier **/ public Series(Parcel in) { String[] data = new String[3]; in.readStringArray(data); this.name = data[0]; this.numOfSeason = Integer.parseInt(data[1]); this.numOfEpisode = Integer.parseInt(data[2]); } @Override public int

Pass ArrayList<? implements Parcelable> to Activity

柔情痞子 提交于 2019-12-17 05:54:53
问题 I have searched a few topics but not found a solution to my problem. public class Series implements Parcelable { private String name; private int numOfSeason; private int numOfEpisode; /** Constructors and Getters/Setters have been removed to make reading easier **/ public Series(Parcel in) { String[] data = new String[3]; in.readStringArray(data); this.name = data[0]; this.numOfSeason = Integer.parseInt(data[1]); this.numOfEpisode = Integer.parseInt(data[2]); } @Override public int