arraylist

JAVA-基础(List及其子类)

怎甘沉沦 提交于 2020-01-16 09:50:20
JAVA-基础(List及其子类) 1.什么是List? java.util.List 接口继承自 Collection 接口,是单列集合的一个重要分支, 习惯性地会将实现了 List 接口的对象称为List集合。 2.list集合的特点? 它是一个元素存取有序的集合。简单来说就是队列方式存取。 它是一个带有索引的集合,通过索引就可以精确的操作集合中的元素(与数组的索引是一个道理)。 集合中可以有重复的元素,通过元素的equals方法,来比较是否为重复的元素。 3.常用方法? public void add(int index, E element) : 将指定的元素,添加到该集合中的指定位置上。 public E get(int index) :返回集合中指定位置的元素。 public E remove(int index) : 移除列表中指定位置的元素, 返回的是被移除的元素。 public E set(int index, E element) :用指定元素替换集合中指定位置的元素,返回值的更新前的元素。 4.list集合的优缺点? 增删慢,查找快。原因:集合数据存储的结构是数组结构。 5.子类? (1)LinkedList?   LinkedList是一个双向链表。增删快,查询慢。   所以他的方法涉及了很多头尾删除方法。   方法:    public void

Java Bubblesort Algorithm

笑着哭i 提交于 2020-01-16 09:36:33
问题 I am trying to use the summer to practice more Java to get better by learning how to code algorithms. I have this problem where I add elements to my ArrayList but somehow the first number I add also sets the number of positions in my list which I want to avoid. I only want the 0th index to contain the number 5. I seem to not catch a clue on how to solve this. public class Algorithms { private ArrayList<Integer> numbers; public Algorithms() { numbers = new ArrayList<Integer>(); numbers.add(5);

Capturing List response for a Retrofit Call with Json body

一个人想着一个人 提交于 2020-01-16 08:46:10
问题 All. I am trying to use Retrofit to send raw json as a body in a POST Request to the server. @POST("api/apps") Call<List<GetApps>> getApp(@Body GetApps body); I have built the model using jsontopojo for the responses that are coming back. Inside my call, I have put the constructors and the getters and setters and also toString(). This is my retrofit call. Retrofit retrofit = new Retrofit.Builder() .baseUrl(APIUrl.BASEURL) .addConverterFactory(GsonConverterFactory.create()) .build();

给Web Services方法传递数组参数

馋奶兔 提交于 2020-01-16 07:19:23
要求数组的长度是动态的,客户端采用ArrayList类型的数组, 例子如下: Web服务端: public void AgreeSp(string sSupper,string sConNo,object[] al) { System.Collections.IEnumerator myal=al.GetEnumerator() ; //获得al中的项 while (myal.MoveNext()) //循环取出所有项 { string sCurrt=myal.Current.ToString(); //当前项 ...... ...... } } 客户端调用: ArrayList al=new ArrayList(); al.Add(....); al.Add(....); _webs.AgreeSp("","",al.ToArray()); 要将ArrayList类型的数组传到Web Services,Web Services的参数类型要定义为object[]数组。 来源: https://www.cnblogs.com/DoNetBird/archive/2005/09/12/235377.html

Arraylist mapping to linkedlist nodes

爱⌒轻易说出口 提交于 2020-01-16 06:27:06
问题 I want to be able to access a certain node in my Doubly Linked List in O(1) time. I know that if i traverse the list to find a certain node it would take O(n) time so I want to map the nodes to an array list where I can access the nodes in O(1) time. I'm really unsure how I would do this mapping. I would like to see an example of how this can be done. Edit: I would like to be able to access any node in the linked list so I can move the nodes around in O(1) time. Example: Move node with ID 5

第十四章 泛型

折月煮酒 提交于 2020-01-16 05:19:36
1.泛型介绍 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。 2.泛型方法 定义泛型方法的规则: 所有泛型方法声明都有一个类型参数声明部分(由尖括号分隔),该类型参数声明部分在方法返回类型之前(在下面例子中的) 每一个类型参数声明部分包含一个或多个类型参数,参数间用逗号隔开。一个泛型参数,也被称为一个类型变量,是用于指定一个泛型类型名称的标识符 类型参数能被用来声明返回值类型,并且能作为泛型方法得到的实际参数类型的占位符。 泛型方法体的声明和其他方法一样。注意类型参数只能代表引用型类型,不能是原始类型(像int,double,char的等) 示例: public class GenericMethodTest { // 泛型方法 printArray public static < E > void printArray ( E [ ] inputArray ) { // 输出数组元素 for ( E element : inputArray ) { System . out . printf ( "%s " , element ) ; } System . out . println ( ) ; } public

Converting arraylist to JSON

寵の児 提交于 2020-01-16 04:47:06
问题 I have an android activity that receives an arraylist from a servlet. Example [stl1010, P3]. When I try to convert this to JSON Object (using the parser class) I get the exception "Error parsing data org.json.JSONException: Value ["stl1010","P3"] of type org.json.JSONArray cannot be converted to JSONObject." Below is the servlet code: private ArrayList<String> ticketLookup(String tkt) { Connection con = null; Statement st = null; //StringArray msga = new StringArray(); ArrayList<String> msga

遍历map及List集合的方法

和自甴很熟 提交于 2020-01-16 03:33:29
import com.xinji.dto.UserDTO; import java.util.ArrayList; import java.util.Iterator; /** * @Author:ella on 2020/1/15. */ public class TestInterator { //遍历List集合的方法 //1.foreach方法 public void iteratorList1(ArrayList<UserDTO> arrayList) { for (UserDTO userDTO : arrayList) { System.out.println("jira:" + userDTO.getJiraNo() + ",cardNo:" + userDTO.getCardNo() + ",gender:" + userDTO.getGender()); } } //2.list.get()方法 public void iteratorList2(ArrayList<UserDTO> arrayList) { for (int i = 0; i < arrayList.size(); i++) { UserDTO userDTO = arrayList.get(i); System.out.println("jira:" + userDTO.getJiraNo(

How to sort List of User-defined type in Java?

Deadly 提交于 2020-01-16 03:31:08
问题 I'm currently learning how to work with Lists in Java. I've come across sorting Strings in a List by using Collections.sort() method and that works fine. However when I create a user-defined datatype, it doesn't sort and gives an error - "no suitable method found for sort(List) method Collections.sort(List,Comparator) is not applicable (cannot instantiate from arguments because actual and formal argument lists differ in length) method Collections.sort(List) is not applicable (inferred type

集合总结

半世苍凉 提交于 2020-01-16 02:38:10
Java集合,不再使用过多的文字描述,为了更加清晰的展示集合之间的关系,本文使用一张导图阐述整个集合,之后会针对几个类似的类,进行对比说明: 1、集合接口有哪些,主要包含哪些实现类 Collection:ArrayList,LinkedList,Vector,HashSet,TreeSet,LinkedHashSet,Queue Map:HashMap,WeekHashMap,TreeMap,ConcurrentHashMap,HashTable 2、ArrayList和Vector的区别 1)Vector使用了Synchronized关键字,是线程安全的,ArrayList是线程不安全的。 2)Vector扩容时2n,ArrayList是1.5n+1 3、ArrayList和LinkedList的区别 1)ArrayList底层为数组,LinkedList为双向链表 2)ArrayList在查找效率更高,而LinkedList在增删效率高 4、ConcurrentHashMap与HashTable的区别 ConcurrentHashMap本身的数据结构与hashMap大致一致,但是引入了分段锁,将锁住某一个segment,而HashTable锁住的是整个结构 5、HashMap与HashTable的区别 1)HashMap是线程不安全的,HashTable是线程安全的 2