arraylist

Java去重

耗尽温柔 提交于 2020-01-12 09:37:20
1.数组去重: private static String[] quChong(String[] arr) { ArrayList<String> list = new ArrayList<String>(); for (String str : arr) { if(!list.contains(str)) list.add(str); } String[] newArr = list.toArray(new String[list.size()]); return newArr; } 2.ArrayList去重: private static ArrayList<String> quChong(ArrayList<String> list) { ArrayList<String> newList = new ArrayList<String>(); for (String str : list) { if(!newList.contains(str)) newList.add(str); } return newList; } private static ArrayList<String> quChong(ArrayList<String> list){ Set<String> set = new HashSet<String>(arrayList); /

Java: How do I sort multiple ArrayList by their size?

天大地大妈咪最大 提交于 2020-01-12 04:44:45
问题 I have 9 different ArrayList and I want to have a list of the top 5. I'm thinking of sorting those ArrayLists by their sizes. Is it possible to do that? If so, how can I achieve that? After a few try i finally got it working, just want to share it with everyone. it will be better to get the size of the arraylist and add it to the big arraylist // creates an ArrayList that holds ArrayLists List allTheLists = new ArrayList(); allTheLists.add(pbaustraliaList.size()); allTheLists.add(pbotherList

day07_Scanner类、Random类、ArrayList类

笑着哭i 提交于 2020-01-12 02:46:07
API 概述 API(Application Programming Interface),应用程序编程接口。Java API是一本程序员的 字典 ,是JDK中提供给我们使用的类的说明文档。这些类将底层的代码实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可。所以我们可以通过查询API的方式,来学习Java提供的类,并得知如何使用它们。 API使用步骤 打开帮助文档。 点击显示,找到索引,看到输入框。 你要找谁?在输入框里输入,然后回车。 看包。java.lang下的类不需要导包,其他需要。 看类的解释和说明。 学习构造方法 使用成员方法。 引用类型使用步骤 类属于引用类型的一种,所有起使用步骤符合引用类型的使用步骤 1:导包 使用import关键字导包,在类的所有代码之前导包,引入要使用的类型,java.lang包下的所有类无需导入。 格式: 2:创建对象 使用该类的构造方法,创建一个该类的对象。 格式: 3:调用 调用该类的成员方法,完成指定功能。 格式: 举例: 调用该类的成员变量,来获取指定的值 格式: 变量名.成员变量名称 Scanner类 什么是Scanner类 一个可以解析基本类型和字符串的简单文本扫描器。 例如,以下代码使用户能够从 System.in 中读取一个数: //备注:System.in 系统输入指的是通过键盘录入数据。

掌握这些,ArrayList就不用再学了(上)

◇◆丶佛笑我妖孽 提交于 2020-01-11 20:09:58
关于ArrayList的学习 ArrayList属于Java基础知识,面试中会经常问到,所以作为一个Java从业者,它是你不得不掌握的一个知识点。😎 可能很多人也不知道自己学过多少遍ArrayList,以及看过多少相关的文章了,但是大部分人都是当时觉得自己会了,过不了多久又忘了,真的到了面试的时候,自己回答的支支吾吾,自己都不满意😥 为什么会这样?对于ArrayList这样的知识点的学习,不要靠死记硬背,你要做的是真的理解它!😁 我这里建议,如果你真的想清楚的理解ArrayList的话,可以从它的构造函数开始,一步步的读源码,最起码你要搞清楚add这个操作,记住,是源码😄 一个问题看看你对ArrayList掌握多少 很多人已经学习过ArrayList了,读过源码的也不少,这里给出一个问题,大家可以看看,以便测试下自己对ArrayLIst是否真的掌握: 请问在ArrayList源码中DEFAULTCAPACITY_EMPTY_ELEMENTDATA和EMPTY_ELEMENTDATA是什么?它们有什么区别? 怎么样?如果你能很轻松的回答上来,那么你掌握的不错,不想再看本篇文章可以直接出门右拐(我也不知道到哪),如果你觉得不是很清楚,那就跟着我继续往下,咱们再来把ArrayList中那些重点过一遍!😎 你觉得ArrayList的重点是啥? 在我看来

how to select multiple items in a listview populated from ArrayList in Android

杀马特。学长 韩版系。学妹 提交于 2020-01-11 14:41:31
问题 I am trying to write a program to select multiple items in the listview i populate. but I am having difficulty of selecting multiple items. Please let me know how to do it. Bellow is how i have populate the Arraylist and i have a custom row with checkbox. i need to get the selected items (name, number) on the button click event. Thank you in advance. I have tried to understand the other posts, but since i was unable to relate them to my code, i am not sure of how to do. public class

Get number of duplicates from ArrayList

社会主义新天地 提交于 2020-01-11 13:59:13
问题 For example, say I have an ArrayList that could contain the following values: x x x y y Now what I want to retrieve is the number of x and x and I want to be able to differentiate what I have, either x or y because in actuality, I could have any object in the ArrayList and I have to be able to tell them apart. What I was thinking of doing was first converting the ArrayList into a LinkedHashSet , which would keep the ordering and remove the duplicates so I would just have x and y But how would

Saving records into the database from the hashmap in android

Deadly 提交于 2020-01-11 12:52:09
问题 I just wanted to ask help for this one. I have a hashmap populate in the listview. the code below is my code for my hashmap: mylist = new ArrayList<HashMap<String, String>>(); for (int i = 0; i < listSelectedFileNames.size(); i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put(FILE_NAME, selectedFileNames[i]); map.put(DESC, ""); map.put(UPLOADED_BY, "User"); map.put(DATE_UPLOADED, myDate); map.put(ACTION, "Delete"); map.put(ID, String.valueOf(i)); map.put(FILE_URI,

Saving records into the database from the hashmap in android

牧云@^-^@ 提交于 2020-01-11 12:51:32
问题 I just wanted to ask help for this one. I have a hashmap populate in the listview. the code below is my code for my hashmap: mylist = new ArrayList<HashMap<String, String>>(); for (int i = 0; i < listSelectedFileNames.size(); i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put(FILE_NAME, selectedFileNames[i]); map.put(DESC, ""); map.put(UPLOADED_BY, "User"); map.put(DATE_UPLOADED, myDate); map.put(ACTION, "Delete"); map.put(ID, String.valueOf(i)); map.put(FILE_URI,

How can Arrays.asList return an instantiated List?

扶醉桌前 提交于 2020-01-11 11:31:51
问题 Arrays.asList returns a typed List. But List is an interface so how can it be instantiated? If try and instantiated a typed List I get an error saying it is not possible. Edit Nevermind I see what's going on, just got confused by the docs for a moment. 回答1: It's an Arrays.ArrayList which shouldn't be confused with java.util.ArrayList . It is a wrapper for the array which means any changes you make, alter the original array, and you can't add or remove entries. Often it is used in combination

How can Arrays.asList return an instantiated List?

馋奶兔 提交于 2020-01-11 11:31:47
问题 Arrays.asList returns a typed List. But List is an interface so how can it be instantiated? If try and instantiated a typed List I get an error saying it is not possible. Edit Nevermind I see what's going on, just got confused by the docs for a moment. 回答1: It's an Arrays.ArrayList which shouldn't be confused with java.util.ArrayList . It is a wrapper for the array which means any changes you make, alter the original array, and you can't add or remove entries. Often it is used in combination