当前位置: 首页 > news >正文

江阴市住房和城乡建设局网站舆情报告2023

江阴市住房和城乡建设局网站,舆情报告2023,百度爱采购下载app,西安网站公司哪家好一、概述 有两种不同的方法来实现List接口。ArrayList类使用基于连续内存分配的实现,而LinkedList实现基于linked allocation。 list接口提供了一些方法: 二、The ArrayList and LinkedList Classes 1.构造方法 这两个类有相似的构造方法&#xff1a…

一、概述

有两种不同的方法来实现List接口。ArrayList类使用基于连续内存分配的实现,而LinkedList实现基于linked allocation

list接口提供了一些方法:

 二、The ArrayList and LinkedList Classes

1.构造方法

这两个类有相似的构造方法:

ArrayList()ArrayList(int initialCapacity)ArrayList(Collection<? extends E> c)
LinkedList()LinkedList(Collection<? extends E> c)

使用第三种构造方法可以将list转为arraylist:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class ArrayListDemo {public static void main(String[] args) {// 创建一个包含一些整数的列表List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);// 使用 ArrayList(Collection<? extends E> c) 构造方法创建一个新的 ArrayList 对象ArrayList<Integer> arrayList = new ArrayList<>(numbers);// 打印 ArrayList 的内容System.out.println(arrayList);}
}

用for循环将数组转为linkedlist: 

// Initialize array of names.String [] namesArray = {"Anna", "Bob", "Carlos", "Debby"};// Create empty list.LinkedList<String> list1 = new LinkedList<>();// Populate the list using a loop.for (String s : namesArray){list1.add(s);}System.out.println(list1);

注意,java中的collection都自带toString方法,结果如下:

[Anna, Bob, Carlos, Debby]

 2.asList

我们可以不用for循环遍历,而直接把一个array转化为linkedlist或arraylist:

// Initialize array of names.
String[] namesArray = {"Anna", "Bob", "Carlos", "Debby"};// Create LinkedList from array using constructor.LinkedList<String> linkedList = new LinkedList<>(Arrays.asList(namesArray));

也可以这样:

LinkedList list2 =new LinkedList<>(Arrays.asList("Anna", "Bob", "Carlos", "Debby"));

3.linkedlist中的其他方法 

三、List Iterators

迭代器可以在列表中向前或向后移动。除了 next() 方法外,列表迭代器还有一个 previous() 方法,列表迭代器有一个光标位置,它总是位于调用 previous() 方法返回的元素和调用 next() 方法返回的元素之间。

最初,光标位于索引为 0 的元素之前。调用 next() 时,光标将返回索引为 0 的元素,并越过返回的元素向前移动到元素 0 和元素 1 之间的位置。此时,调用 previous() 将返回元素 0,并将光标移回起点,但调用 next() 将返回元素 1,并将光标向前移动到元素 1 和元素 2 之间的位置。如果光标已经越过最后一个元素,调用 next() 将抛出 NoSuchElementException 异常。同样,如果光标位置在元素 0 之前,调用 previous() 也会产生同样的异常。

列表迭代器中的 remove() 方法会从列表中删除最后一次调用 next() 或 previous() 时返回的元素

也就是说,如果在调用 `remove()` 方法之前没有先调用 `next()` 或 `previous()` 方法,那么会抛出一个 `IllegalStateException` 异常。

这是因为 `remove()` 方法的作用是删除迭代器最后访问的元素,所以在调用 `remove()` 方法之前必须先调用 `next()` 或 `previous()` 方法,以确保迭代器已经指向了要删除的元素。

如果在调用 `remove()` 方法之前没有先调用 `next()` 或 `previous()` 方法,那么迭代器没有指向任何元素,也就无法删除元素,因此会抛出 `IllegalStateException` 异常。

这里的迭代器的remove方法和list的remove方法不是一个东西,自然不能按原来的方式理解。

list的迭代器还提供了以下方法:

 例子:

import java.util.*;/*** Demonstrates the list iterator*/
public class ListIteratorDemo {public static void main(String[] args) {// Create an array list to hold strings.String[] names = {"Chris", "David", "Katherine", "Kenny"};List<String> nameList = new ArrayList<>(Arrays.asList(names));// Display the names in the list.System.out.println("Here are the original names.");System.out.println(nameList);// Get a list iterator.ListIterator<String> it = nameList.listIterator();// Add "Darlene" to the list right after "Chris".while (it.hasNext()) {String str = it.next();// If the last name retrieved was "Chris"// then insert "Darlene".if (str.equalsIgnoreCase("Chris")) {it.add("Darlene");// We are donebreak;}}//Display the names in the list again.System.out.println("\nHere are the new names now.");System.out.println(nameList);}
}

Here are the original names.

[Chris, David, Katherine, Kenny]

Here are the new names now.

[Chris, Darlene, David, Katherine, Kenny]

 四、用list接口变量去引用linkedlist或arraylist

推荐使用

List<String> nameList = new ArrayList<>();

而不是

 ArrayList<String> nameList = new ArrayList<>();

因为第一种方法后期容易修改。

比如在后期发现namelist实际上应该使用linkedlist,这是只需要把第一句改成:

List<String> myList = new LinkedList<>();

这时,项目中所有的myList就都变成了Linkedlist。

如果用第二种方法,我们在项目中别的地方传参的时候,会传入(arraylist nameList),这时如果要换成Linkedlist,我们需要改变所有传入参数的类型。

使用第一种方法,我们传的参数是(List nameList),就不需改变。

http://www.yayakq.cn/news/405335/

相关文章:

  • 外国做电子产品网站有哪些html网站支付链接怎么做的
  • 大型企业网站建设制作wordpress媒体默认链接
  • 站长平台链接提交免费网络营销方式
  • 哪些网站可以做设计软件景区旅游网站平台建设
  • 九江专业网站建设定制建设银行江苏官网招聘网站
  • 怎么才能访问自己做的网站自考
  • 全国教育平台网站建设做婚礼邀请函网站
  • wordpress评论差价上海有什么seo公司
  • 什么网站的注册是动态公司简介通用模板
  • 高邮城乡建设局 网站重庆营销型网站设计
  • 网页版游戏排行榜田田田田田田田田2022年seo最新优化策略
  • 做网站的职位桂林小程序制作
  • cvv钓鱼网站怎么做有效方法的小企业网站建设
  • 有没有教做熟食的网站泰州建设企业网站
  • 企业网站建设可行性青岛大学网站建设
  • 工作室网站源码做网站用jsp还是j2ee
  • 供热设施网站搭建教程企业如何全面开展品牌工程建设
  • 企业服务网站建设方案WORDPRESS如何播放视频
  • 湖北城乡建设网站旅游网站建设的功能定位
  • 如何免费做网站详细点说滑板网页网站设计
  • 平面设计适合女生学吗网站优化推广服务
  • 南阳网站建设培训学校wordpress角色
  • struts2 做的网站游戏网站平台怎么做的
  • 58同城新密网站建设纳溪区城乡住房建设局网站
  • 网站单页在线制作软件山东省山东省建设厅网站首页
  • tq网站建设google官网入口手机版
  • 莱芜正规的企业建站公司百度权重站长工具
  • 高端网站建设收费为何比较贵wordpress 相册 不显示图片
  • 学做蛋糕有哪些网站百度竞价渠道代理
  • 宁波建设局网站自己买域名建设网站