arraylist

How to Get Values from List of HashMap?

岁酱吖の 提交于 2020-01-14 11:58:28
问题 I have a List of HashMap 's which has key of type Integer and value of type Long . List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>(); for (int i = 0; i < 10; i++) { HashMap<Integer, Long> mMap = new HashMap<Integer, Long>(); mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i)); ListofHash.add(mMap); } Now, how do I retrieve the key and value from the list of HashMap ? If using Collection class is the solution, please enlight me. Update 1: Actually i am

Insert into an already-sorted list

China☆狼群 提交于 2020-01-14 09:48:08
问题 With Java, I have a class, known as TestClass, which has a member named Name, which is a string. I also have an ArrayList of this type, which is already sorted alphabetically by Name. What I want to do is find the best index in which to put a new instance of TestClass. The best approach I could come up with so far is this: public static int findBestIndex(char entry, ArrayList<TestClass> list){ int desiredIndex = -1; int oldPivot = list.size(); int pivot = list.size()/2; do { char test = list

C# is there ever a reason to use ArrayList instead of List<T> anymore?

点点圈 提交于 2020-01-14 08:51:06
问题 The title pretty much sums it up. Now that we have List<T> 's why would anyone want to use an ArrayList instead? The only reason I can think of is if you are forced to use an old version of .NET before List<T> was implemented? 回答1: As you said, if for some reason you are stuck with .Net 1.1 then you don't have a choice. Your question seems to indicate that there is a choice. So then there is no reason to user ArrayList . Anything that ArrayList can do, List<T> can do as well, if not better.

Java Finding a value in arraylist

非 Y 不嫁゛ 提交于 2020-01-14 06:57:08
问题 I have added few number into the arraylist . I would want to find the certain value from it.Example i have 4,4,9,9,18. I would like to find the value of 26. If 26 > largest value in the list it will display 18 and if value is 17 it will display 9, and if value is 5 it will display 4. Also is there another method to implement this search because liner search might be slow. search value 26 [4,4,9,9,18] display 18 [20,20,29,29,4] display 20 [28,28,28,1,10] display 28 if you have this list and

Java read text file in lines, but seperate words in lines into array

耗尽温柔 提交于 2020-01-14 06:05:51
问题 I am trying to write a code that looks at a text file, and does a while loop so that it reads each line, but I need each word per line to be in an array so I can carry out some if statements. The problem I am having at the moment is that my array is currently storing all the words in the file in an array.. instead of all the words per line in array. Here some of my current code: public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("test.txt")); List

Java read text file in lines, but seperate words in lines into array

╄→гoц情女王★ 提交于 2020-01-14 06:05:23
问题 I am trying to write a code that looks at a text file, and does a while loop so that it reads each line, but I need each word per line to be in an array so I can carry out some if statements. The problem I am having at the moment is that my array is currently storing all the words in the file in an array.. instead of all the words per line in array. Here some of my current code: public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("test.txt")); List

Using an interface to apply method to ArrayList

╄→尐↘猪︶ㄣ 提交于 2020-01-14 03:08:27
问题 I'm reviewing for an exam. A question on an old test was to: Using an interface, write a method that applies an arbitrary method to every element of an ArrayList. Both the arraylist and method are the parameters to the method. Would a workable solution be: public <object> someMethod() { scanner GLaDOS = new (someArray); someArray.useDelimiter(","); for (i = 0; i < someArray.length; i++) { someOtherMethod(someArray[i]); } } 回答1: I would've expected something like this: // define the interface

原型模式

穿精又带淫゛_ 提交于 2020-01-14 02:26:12
原型模式 定义: 用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。 类型: 创建类模式 类图: 原型模式主要用于对象的复制,它的核心是就是类图中的原型类Prototype。Prototype类需要具备以下两个条件: 实现Cloneable接口。在java语言有一个Cloneable接口,它的作用只有一个,就是在运行时通知虚拟机可以安全地在实现了此接口的类上使用clone方法。在java虚拟机中,只有实现了这个接口的类才可以被拷贝,否则在运行时会抛出CloneNotSupportedException异常。 重写Object类中的clone方法。Java中,所有类的父类都是Object类,Object类中有一个clone方法,作用是返回对象的一个拷贝,但是其作用域protected类型的,一般的类无法调用,因此,Prototype类需要将clone方法的作用域修改为public类型。 原型模式是一种比较简单的模式,也非常容易理解,实现一个接口,重写一个方法即完成了原型模式。在实际应用中,原型模式很少单独出现。经常与其他模式混用,他的原型类Prototype也常用抽象类来替代。 实现代码: class Prototype implements Cloneable { public Prototype clone(){ Prototype prototype = null;

Java 对象排序详解

浪尽此生 提交于 2020-01-14 01:08:08
更多内容关注公众号:SAP Technical 各位可以关注我的公众号:SAP Technical 很难想象有Java开发人员不曾使用过Collection框架。在Collection框架中,主要使用的类是来自List接口中的ArrayList,以及来自Set接口的HashSet、TreeSet,我们经常处理这些Collections的排序。 在本文中,我将主要关注排序Collection的ArrayList、HashSet、TreeSet,以及最后但并非最不重要的数组。 让我们看看如何对给定的整数集合(5,10,0,-1)进行排序: 数据(整数)存储在ArrayList中 private void sortNumbersInArrayList() { List<Integer> integers = new ArrayList<>(); integers.add(5); integers.add(10); integers.add(0); integers.add(-1); System.out.println("Original list: " +integers); Collections.sort(integers); System.out.println("Sorted list: "+integers); Collections.sort(integers,

Java array: Attribute size?

折月煮酒 提交于 2020-01-13 19:39:32
问题 I found this answer on Stack Overflow: length is constant which is used to find out the array storing capacity not the number of elements in the array Example: int a[] = new int[5] a.length always returns 5 which is called the capacity of an array, so length always returns the CAPACITY. but "number of elements in the array is called size" Example: int a[] = new int[5] a[0] = 10 Will result in a.size = 1 and a.length = 5 . size() works with collection, length works with arrays in java