Array ListArray List < E > 泛型类ArrayList常用的API
集合与数组类似,也是一种容器。
数组的不足:
1.大小固定。(长度固定)
2.增删操作不便利。
数组的不足集合可以很好的解决。
集合的大小不固定,是动态变化的。类型也可以选择不固定。
集合提供了很多API,而数组的功能比较单一。
Array List 集合的一种,支持索引。 类似于数组。
Array List的使用方法
| 构造器 | |
|---|---|
| public ArrayList() | 构建一个空的集合对象。 |
| 元素的添加 | |
|---|---|
| add(E e) | 将元素添加到末尾 |
| add(int index,E e) | 在指定位置添加元素 |
因为Java是一种强类型的语言,所以在定义集合时一般使用泛型的定义方法。
在操作使要求集合只能操作该种数据类型。
| Array List < String > | 此集合只操作字符串类型的元素 |
|---|---|
| Array List < Intege> | 此集合只操作整数类型的元素 |
集合只能存贮引用类型(对象)的元素,不能存储基本数据类型
int 基本数据类型 Intege 引用数据类型
现在已经支持int 直接转为 Integer
int a = 3;
Integer b = a;
System.out.println(b);
泛型的使用格式
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<>();
a.add(1);
a.add(1.3);
a.add("s");
a.add(false);
a.add('s');
a.add('s');
a.add("ahahah");
a.add(0,'中');
System.out.println(a.add(65498432));
System.out.println(a);
for (int i = 0; i < a.size(); i++) {
System.out.println(a.get(i));
}
已经规定了集合a只能操作整型数据,所以除了a.add(1)这一操作正确外,其他的操作都使错误的。
从JDK1.7之后泛型支持ArrayList< Integer > a = new ArrayList<>(); 可以在后面省略<>中的类型
| 方法名称 | 说明 |
|---|---|
| public E get(int index) | 返回指定索引处的元素 |
| public int size() | 返回集合中的元素的个数 |
| public E remove(int index) | 删除指定索引处的元素,返回被删除的元素 |
| public boolean remove(Object o) | 删除指定的元素,返回删除是否成功 |
| public E set(int index,E element) | 修改指定索引处的元素,返回被修改的元素 |
这似乎是一个基本问题,但我无法在任何地方找到它。基本上我有一个像这样的XML链接列表:(全部在一个字符串中)我已经有了包含所有XML的“字符串”变量。只需提取HTML字符串。http://photos-a.ak.fbcdn.net/hphotos-ak-ash4/486603_10151153207000351_1200565882_t.jpghttp://photos-c.ak.fbcdn.net/hphotos-ak-ash3/578919_10150988289678715_1110488833_t.jpg我想将它们转换成数组列表,所以像URLArray[0]这样的东西将作为字符
我在Jackson生成的XML输出中得到了两个包装器元素。我只想拥有一个。我有一个Javabean@Entity@Table(name="CITIES")@JacksonXmlRootElement(localName="City")publicclassCityimplementsSerializable{privatestaticfinallongserialVersionUID=21L;@Id@GeneratedValue(strategy=GenerationType.AUTO)@JacksonXmlProperty(isAttribute=true)privateLongid
我想通过JAX-WS返回包含List对象的ArrayList。这是我的方法publicArrayListallTokens(){ArrayListallItems=newArrayList();ListmyList1=newArrayList();myList1.add("Indunil");myList1.add(22);ListmyList2=newArrayList();myList2.add("Tharanga");myList2.add(20);//addingliststoallitemsallItems.add(myList1);allItems.add(myList2)
我正在解析一些xml,现在我正在尝试获取一些节点的文本值。这是xmlPlainBagelCheeseBagel现在我正在使用XMLPullParser及其在获取文本区域以外的工作方式。所以在获取文本的情况下,我有这个:caseXmlResourceParser.TEXT:itemsArray.add(xmlData.getText());Log.i(TAG,"a"+xmlData.getText()+"b");break;所以它添加了PlainBagel和CheeseBagel项,很棒,但是在onProgressUpdate方法中,当我记录结果时,我看到了这个:[,,,PlainBag
我正在尝试将XML反序列化为对象,但我遇到了一种情况。任何人都可以在这里帮助我。XML:代码:[XmlRoot("Level")]publicclassLData{[XmlArray("Warp_Blocks")][XmlArrayItem("Warp_Block",typeof(WarpBlock),IsNullable=false)]publicListWarpBlocks;}publicclassLBlock{[XmlAttribute("row")]publicintrow;[XmlAttribute("col")]publicintcol;}publicclassWarpBl
我有类似的东西@XmlElementWrapper(name="Mylist")ListmyItems=newArrayList()结果是item1item2item3有没有可能让它更像item1,item2,item3既然我要找的数据都是文本数据? 最佳答案 您可以使用@XmlList使其成为一个空格分隔的值。对于逗号分隔的列表,您需要使用XmlAdapter.有关XmlAdapter的更多信息见:http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon
我有两个类,它们之间的关系如下。我有一个ObjReal对象列表,其中显示了整个数据。我想在不丢失对象关系的情况下将整个列表保存到xml中。我的意思是用XML表示的对象应该有相应的objStrucs。将ObjReal存储到xml中很简单,但我对它的ObjStruc关系感到困惑。请帮助解决这个问题。我的ObjReal是:ClassObjReal{privateStringid;privateStringdata;ArrayListobjStrucs=newArrayList();publicArrayListgetObjStrucs(){returnobjStrucs;}publicStr
我有6个数组列表,我想在不使用一堆IF语句的情况下知道哪个是最长的。“ifarraylist.count>anotherlist.countThen...”VB.net或C#.Net(4.0)中的示例会很有帮助。arraylist1.countarraylist2.countarraylist3.countarraylist4.countarraylist5.countarraylist6.countDIMlongestAsinteger=....'thelongestarraylistshouldbestoredinthisvariable.谢谢 最佳答案
所以我将以下ArrayList存储在$var中:ip_prefixregionstring0.0.0.0/24GLOBALSomething0.0.0.0/24GLOBALSomething0.0.0.0/24GLOBALSomething0.0.0.0/24GLOBALSomethingIneedtoaddarowtothishoweverthefollowingcodereturnsanerror:$var.add("127.0.0.1/32","GLOBAL","something")错误:Cannotfindanoverloadfor"Add"andtheargumentcou
我正在忙着准备MCTS70-536考试,根据考试书(MicrosoftPress-.NETFramework-ApplicationDevelopmentFoundationSelfPacedTrainingKit2ndEdition),这段代码示例:ArrayListal=newArrayList();al.AddRange(newstring[]{"Hello","world","this","is","a","test"});Console.WriteLine(al.BinarySearch("this"));将值“2”输出到控制台,因为项目“this”位于索引2。同意这是我运行