依赖注入的方式测试ArrayList和LinkedList的效率(对依赖注入的再次理解)

时间:2023-03-10 06:09:22
依赖注入的方式测试ArrayList和LinkedList的效率(对依赖注入的再次理解)

9/20 号再进行学习

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

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

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

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

先贴结果

依赖注入的方式测试ArrayList和LinkedList的效率(对依赖注入的再次理解)


项目结构

依赖注入的方式测试ArrayList和LinkedList的效率(对依赖注入的再次理解)


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>