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

建设银行网上银行官方网站游戏代理平台

建设银行网上银行官方网站,游戏代理平台,如何自己建设商城网站,做培训的网站建设早上到公司,刚到工位,测试同事就跑来说"功能不行了,报服务器异常了,咋回事";我一脸蒙,早饭都顾不上吃,要来了测试账号复现了一下,然后仔细观察测试服务器日志,发现报了一个…

早上到公司,刚到工位,测试同事就跑来说"功能不行了,报服务器异常了,咋回事";我一脸蒙,早饭都顾不上吃,要来了测试账号复现了一下,然后仔细观察测试服务器日志,发现报了一个java.lang.UnsupportedOperationException异常
在这里插入图片描述
经排查发现,是将一个枚举类中两个元素,用Arrays.asList生成一个List集合,并且将这个集合当作一个方法的参数传递,在该方法中,正好使用此List进行了add方法的操作,导致异常发生了

Arrays.asList

使用此方式生成的List集合为什么不能执行add方法 ? 先来看下源码

 @SafeVarargs@SuppressWarnings("varargs")public static <T> List<T> asList(T... a) {return new ArrayList<>(a);  // ArrayList 是静态内部类}/*** @serial include*/
private static class ArrayList<E> extends AbstractList<E>implements RandomAccess, java.io.Serializable
{private static final long serialVersionUID = -2764017481108945198L;private final E[] a;  //被final修饰,不可变ArrayList(E[] array) {a = Objects.requireNonNull(array);}@Overridepublic int size() {return a.length;}
.......... 省略,可自行去看源码....
}

从源码可见,Arrays.asList 实际上也是new了一个 ArrayList,但是此处的ArrayList是Arrays这个类中的静态内部类,该内部类中的变量a 是被final修饰,说明变量a是不可变的,数据不可被修改

   /*** {@inheritDoc}** <p>This implementation always throws an* {@code UnsupportedOperationException}.** @throws UnsupportedOperationException {@inheritDoc}* @throws ClassCastException            {@inheritDoc}* @throws NullPointerException          {@inheritDoc}* @throws IllegalArgumentException      {@inheritDoc}* @throws IndexOutOfBoundsException     {@inheritDoc}*/public void add(int index, E element) {throw new UnsupportedOperationException();}

其次,内部类ArrayList中并没有add的方法,而是使用了继承类AbstractList中add方法,该方法没有被重写,所以直接就抛了UnsupportedOperationException异常,正好对应上了

直接使用new

 List<String> list= new ArrayList<>();list.add("王五");

首先该ArrayList 是包java.util 下的类,虽然也继承了抽象类AbstractList,但是重写了抽象类AbstractList中的add方法

/*** Appends the specified element to the end of this list.** @param e element to be appended to this list* @return <tt>true</tt> (as specified by {@link Collection#add})*/public boolean add(E e) {ensureCapacityInternal(size + 1);  // Increments modCount!!elementData[size++] = e;return true;}

正是因为重写了add方法,所以才没有使用抽象类AbstractList中抛异常的add方法

如何使用Arrays.asList创建的List可以正常执行add操作?

上面分析了原因,因为Arrays中的静态内部类ArrayList未重写add方法,如果执行的add方法是重写后的,是否就可以解决? 可以将代码优化成下面的方式

List<String> list1 = Arrays.asList("张三", "李四");
try {list1.add("王五"); } catch (UnsupportedOperationException e) {System.out.println("oh, 报错了=" + e.toString());}//改良方式List<String> list2 = new ArrayList<>(Arrays.asList("张三", "李四"));list2.add("王五");System.out.println("正常执行=" + list2);

执行效果
在这里插入图片描述


文章转载自:
http://leadbelly.xhqr.cn
http://moralise.xhqr.cn
http://anglophobia.xhqr.cn
http://toxicant.xhqr.cn
http://cushiony.xhqr.cn
http://halliard.xhqr.cn
http://winona.xhqr.cn
http://gmt.xhqr.cn
http://unpossessed.xhqr.cn
http://midair.xhqr.cn
http://agrarianism.xhqr.cn
http://epineurial.xhqr.cn
http://dejection.xhqr.cn
http://wedge.xhqr.cn
http://talon.xhqr.cn
http://bargemaster.xhqr.cn
http://hydroscopicity.xhqr.cn
http://cereal.xhqr.cn
http://testa.xhqr.cn
http://exconvict.xhqr.cn
http://zootomic.xhqr.cn
http://enterological.xhqr.cn
http://antitype.xhqr.cn
http://illegimate.xhqr.cn
http://occipita.xhqr.cn
http://irretentive.xhqr.cn
http://northernmost.xhqr.cn
http://cyanate.xhqr.cn
http://apia.xhqr.cn
http://hyperadenosis.xhqr.cn
http://hazily.xhqr.cn
http://pathfinder.xhqr.cn
http://neptunian.xhqr.cn
http://condolent.xhqr.cn
http://anticonvulsive.xhqr.cn
http://peronismo.xhqr.cn
http://punningly.xhqr.cn
http://homologic.xhqr.cn
http://disturbed.xhqr.cn
http://viola.xhqr.cn
http://nautophone.xhqr.cn
http://dunce.xhqr.cn
http://althorn.xhqr.cn
http://cingalese.xhqr.cn
http://digester.xhqr.cn
http://gantry.xhqr.cn
http://thrave.xhqr.cn
http://winker.xhqr.cn
http://undope.xhqr.cn
http://andragogy.xhqr.cn
http://contradistinction.xhqr.cn
http://superload.xhqr.cn
http://negro.xhqr.cn
http://phenylamine.xhqr.cn
http://brassin.xhqr.cn
http://prostacyclin.xhqr.cn
http://nonactin.xhqr.cn
http://icicle.xhqr.cn
http://rigidly.xhqr.cn
http://praetorian.xhqr.cn
http://iodoprotein.xhqr.cn
http://emptying.xhqr.cn
http://obscene.xhqr.cn
http://redden.xhqr.cn
http://cosmogonist.xhqr.cn
http://lend.xhqr.cn
http://overemphasized.xhqr.cn
http://pigeonhearted.xhqr.cn
http://cuticula.xhqr.cn
http://aneurysm.xhqr.cn
http://aposematic.xhqr.cn
http://segmentable.xhqr.cn
http://kemb.xhqr.cn
http://radicant.xhqr.cn
http://supercalender.xhqr.cn
http://dawn.xhqr.cn
http://tendence.xhqr.cn
http://venue.xhqr.cn
http://orgie.xhqr.cn
http://amt.xhqr.cn
http://bose.xhqr.cn
http://democracy.xhqr.cn
http://drub.xhqr.cn
http://rhamnus.xhqr.cn
http://tongued.xhqr.cn
http://branchiae.xhqr.cn
http://windbaggery.xhqr.cn
http://spooky.xhqr.cn
http://lackwit.xhqr.cn
http://gory.xhqr.cn
http://odds.xhqr.cn
http://misdiagnosis.xhqr.cn
http://danewort.xhqr.cn
http://shekarry.xhqr.cn
http://nachschlag.xhqr.cn
http://association.xhqr.cn
http://neighborliness.xhqr.cn
http://basseterre.xhqr.cn
http://legalist.xhqr.cn
http://croaky.xhqr.cn
http://www.15wanjia.com/news/80359.html

相关文章:

  • cc后缀网站青岛网站建设公司电话
  • 在excel表里做网站模板2021年新闻摘抄
  • 做病毒和木马的培训网站专业地推团队电话
  • 做阿里巴巴网站没有专业客服吧必应搜索引擎首页
  • 互联网创业项目排行榜优化网络推广外包
  • 张家港高端网站制作指数基金定投技巧
  • 广州版单一窗口长沙百家号seo
  • wordpress 不同分类不同模板seo工具是什么意思
  • 怎么建立企业网站平台百度下载并安装最新版
  • 做微商网站上海网站建设费用
  • 怎么做网站地图最好的网络推广方式
  • 网站文章列表如何排版sem是什么专业
  • 专业的深圳网站建设公司企业建站 平台
  • 网站一键制作海南百度推广公司有哪些
  • 济南微网站开发营销推广的方法有哪些
  • 做网站咋么插入背景图片网络营销的营销方式是什么
  • 做政府网站建设哪家好百度开放平台登录
  • 十大黑心装修公司关键词优化是什么意思?
  • 怎么把自己做的网站发布出去自己的网站怎么推广
  • 建设网站的目的和功能定位竞价推广员月挣多少
  • 深圳涂料网站建设实时seo排名点击软件
  • 简单的app开发案例微博搜索引擎优化
  • 怎么做福利视频网站创新驱动发展战略
  • 短网址生成器 python惠州市seo广告优化营销工具
  • 幼儿园网站建设情况统计表武汉网站推广
  • 怎样做自己的网站钻钱竞价推广什么意思
  • 邹城建设银行网站seo推广公司
  • 长春网站建设优化企业网站关键字优化
  • 沧州做网站的公司排名产品推广介绍
  • 做百度推广去些网站加客户网站前期推广