9/20 号再进行学习

在C++中,main函数尽可能的简单,只要调用子函数的一句话就实现了功能。

java开发中,controller就相同于是main函数,其他类的方法不在本类中时候,

1.可以用new对象的方法,调用那个方法

2.用依赖注入的方式,将其他类的对象注入到main函数的类中,然后就可以调用这个方法了

先贴结果


项目结构


8/22添加
链表的优势是插入和删除,劣势是查找
数组的优势的查找,劣势的插入删除
在9999999*2个整数中进行查找的效率对比
ArrayListTest used a total time is 1 millisecondes !(有时测出来是0)
linkedList used a total time is 15 millisecondes !

从序列中删除一个数字的效率对比
ArrayListTest used a total time is 15 millisecondes !
linkedList used a total time is 0 millisecondes !

使用配置文件的方式

package com.baobaotao1;
import java.util.Date;
import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
//往这个类里面注入了2个对象
private List<Integer> list01;
private List<Integer> list02; public void setList01(List<Integer> list01) {
this.list01 = list01;
}
public void setList02(List<Integer> list02) {
this.list02 = list02;
}
  public  void vs(List<Integer> list){
long t1 = new Date().getTime();
list.add(1); list.add(2);list.add(3);
for(int i=1;i<99999;i++){
list.add(1,i);
}
long t2 = new Date().getTime();
System.out.println("时间是"+(t2-t1));
} 
  public void test(){
    this.vs(list01);
    this.vs(list02);
  }
  public static void main(String[] args) {
    //new CarTest().test();
   //上面的这种测试的话,加载不了配置文件,空指针异常,在tomcat服务器中运行的web项目中会自动加载
// web项目中,要么自动去加载xml文件,要么根据web.xml文件的配置去加载application.xml文件
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
Test c = (Test) ctx.getBean("carTest");
c.test();
} }
在WEB-INF 这个目录下,这个文件夹下的web.xml文件会被自动加载并读取,
回答:tomcat会自动加载web.xml文件,部署在tomcat的WEB-INF文件夹下的.xm文件,
只有web.xml文件会被自动加载,如在这个目录下还有其他的xml文件,需要在web.xml文件里配置

配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="list1" class="java.util.ArrayList" ></bean>
<bean id="list2" class="java.util.LinkedList"></bean> <bean id="carTest" class="com.baobaotao1.CarTest">
<property name="list01" ref="list1"/>
<property name="list02" ref="list2"/>
</bean> </beans>



注解进行测试

package com.baobaotao1;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
/*下面的这2个注解用于 new ClassPathXmlApplicationContext*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test {
@Resource //按照list01这个名称进行注入
private List<Integer> list01;
@Resource
private List<Integer> list02;
//面向接口编程List<Integer>完全降低了耦合,完全没有实现类的影子
public void vs(List<Integer> list){
long t1 = new Date().getTime();
list.add(1); list.add(2);list.add(3);
for(int i=1;i<9999;i++){
list.add(1,i);
}
long t2 = new Date().getTime();
System.out.println("时间是"+(t2-t1));
} public void test(){
this.vs(list01);
this.vs(list02);
} public static void main(String[] args) {
// new CarTest().test(); 用这种方式去加载xml配置文件的话,也会空指针
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
Test c = (Test) ctx.getBean("carTest");
c.test();
} }

配置文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!--这句话必须有,以及上面的那些必须有(这是进行注解开发所必须要的)-->
<context:component-scan base-package="com.baobaotao1"></context:component-scan> <bean id="list01" class="java.util.ArrayList" ></bean>
<bean id="list02" class="java.util.LinkedList"></bean>
<bean id="carTest" class="com.baobaotao1.Test"></bean>
//传入这个包名+类名 一般用到的都是反射机制
</beans>

依赖注入的方式测试ArrayList和LinkedList的效率(对依赖注入的再次理解)的更多相关文章

  1. 老徐和阿珍的故事:ArrayList和LinkedList的效率到底哪个高?

    人物背景: 老徐,男,本名徐福贵,从事Java相关研发工作多年,职场老油条,摸鱼小能手,虽然岁数不大但长的比较着急,人称老徐.据说之前炒某币败光了所有家产,甚至现在还有欠债. 阿珍,女,本名陈家珍,刚 ...

  2. Webservice WCF WebApi 前端数据可视化 前端数据可视化 C# asp.net PhoneGap html5 C# Where 网站分布式开发简介 EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下? SQL Server之深入理解STUFF 你必须知道的EntityFramework 6.x和EntityFramework Cor

    Webservice WCF WebApi   注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下, ...

  3. Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件

    1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...

  4. MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息

    MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...

  5. java的list几种实现方式的效率(ArrayList、LinkedList、Vector、Stack),以及 java时间戳的三种获取方式比较

    一.list简介 List列表类,顺序存储任何对象(顺序不变),可重复. List是继承于Collection的接口,不能实例化.实例化可以用: ArrayList(实现动态数组),查询快(随意访问或 ...

  6. MVVM模式用依赖注入的方式配置ViewModel并注册消息

    最初的想法 这次主要讨论下给View指定ViewModel的事情.一般来说给View指定ViewModel常用的方式有两种,一种是在View的后台代码中写DataContext = new ViewM ...

  7. 极简SpringBoot指南-Chapter02-Spring依赖注入的方式

    仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...

  8. 基准测试了 ArrayList 和 LinkedList ,发现我们一直用 ArrayList 也是没什么问题的

    ArrayList 应该是 Java 中最常用的集合类型了,以至于我们说到集合就会自然而然的想到 ArrayList.很多同学都没有用过除了 ArrayList 之外的其他集合,甚至于都已经忘了除了 ...

  9. ArrayList、LinkedList、Vector、Array和HashMap、HashTable

    就 ArrayList 与 Vector 主要从二方面来说. 一.同步性:Vector 是线程安全的,也就是说是同步的,而ArrayList 是线程序不安全的,不是同步的 二.数据增长:当需要增长时, ...

随机推荐

  1. SpringWeb项目常用注解简单介绍

    注解分为两类: 一类是使用Bean,即是把已经在xml文件中配置好的Bean拿来用,完成属性.方法的组装:比如@Autowired , @Resource,可以通过byTYPE(@Autowired) ...

  2. Factory,工厂设计模式,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  3. JSP动态网页

    01.什么是服务器 02.什么是动态网页  动态网页是指在服务器端运行的,使用程序语言设计的交互式网页,它们会根据某种条件的变化,返回不同的网页内容.可以让用户和服务器交互的网站 动态网站可以实现交互 ...

  4. Azulão--青鸟--IPA--巴西葡萄牙语

    这是巴西很有名的民谣.

  5. Xilinx 7 series FPGA multiboot技术的使用(转)

    reference:https://www.cnblogs.com/chensimin1990/p/9067629.html 当升级程序有错误的时候,系统会启动golden bitstream 注意: ...

  6. DevExpress ASP.NET v18.2新功能详解(三)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Cont ...

  7. 第一个python程序--hello,world

    Hello World程序 在linux 下创建一个文件叫hello.py,并输入 print ('hello,word') 然后执行:python3 hello.py ,输出: [root@IDC- ...

  8. Python 实例变量

    class Person: def __init__(self, name, id, gender, birth): self.name = name # 实例变量 对象里的变量 self.id = ...

  9. 初识爬虫见到的两个类 BufferedWriter和 BufferedReader

    BufferedWriter 和 BufferedReader 为带有默认缓冲的字符输出输入流,因为有缓冲区所以很效率比没有缓冲区的高. 使用BufferedWriter和BufferedReader ...

  10. python实现数组和链表的归并排序

    归并排序是一种稳定的排序,采用分而治之策略,可以用于顺序储存结构,也易于在链表上实现.其原理如下图: 算法时间复杂度为  O(nlogn),空间复杂度为 O(n). 1 在数组上实现 def merg ...