发表于: 2018-03-04 18:49:24
1 726
今天完成的事情:
分享微信公众号——后端初学者
学习Java基础——集合框架
数组的局限性:如果要存放多个对象,可以使用数组,但是数组有局限性。比如声明长度是10的数组,不用的数组就浪费了,超过10的个数,又放不下
ArrayList存放对象:为了解决数组的局限性,引入容器类的概念。最常见的容器类就是ArrayList。容器的容量”capacity”会随着对象的增加,自动增长,只需要不断往容器里增加即可,不用担心出现数组的边界问题。
常见用法:
1 增加。
第一种是直接add对象,把对象加在最后面。例如:heros.add(new Hero(“hero”+I));
第二种是在指定位置加对象。例如:heros.add(3,specialHero);
2 判断是否存在
通过方法contains判断一个对象是否在容器中
判断标准:是否是同一个对象,而不是name是否相同
例如:heros.contains(special hero);
3 获取指定位置的对象
通过get获取指定位置的对象,如果输入的下标越界,一样会报错
例如:heros.get(5);
4 获取对象所处的位置
indexOf用于判断一个对象在ArrayList中所处的位置,与contains一样,判断标准是对象是否相同,而非对象的name值是否相等
例如:heros.indexOf(specialHero);
5 删除
remove用于把对象从ArrayList中删除
方法一:根据下标删除ArrayList的元素,heros.remove(2);
方法二:根据对象删除,heros.remove(specialHero);
6 替换
set用于替换指定位置的元素
例如:heros.set(5, new Hero(“hero 5”);
7 获取大小
size用于获取ArrayList的大小
例如:heros.size();
8 转换为数组
toArray可以把一个ArrayList对象转换为数组,需要注意的是,如果要转换为一个Hero数组,那么需要传递一个Hero数组类型的对象给toArray(),这样toArray方法才知道,你希望转换为哪种类型的数组,否则只能转换为Object数组
例如:heros.toArray(new Hero[]{});
9 把另一个容器所有对象都加进来
addAll 把另一个容器所有对象都加进来
例如:heros.addAll(anotherHero);
10 清空
clear清空一个ArrayList
例如:heros.clear();
List接口
ArrayList实现了接口List,常见的写法会把引用声明为接口List类型
注意:是java.util.List,而不是java.awt.List
ArrayList实现了List接口,所以List接口的方法ArrayList都实现了
泛型 Generic
不指定泛型的容器,可以存放任何类型的元素;指定了泛型的容器,只能存放指定类型的元素及其子类
泛型的简写:为了不使编译器出现警告,需要前后都使用泛型,
例如:List<Hero>generichreos = new ArrayList<Hero>();
遍历ArrayList的方法:
1 用for循环遍历
List<Hero> heros = new ArrayList<Hero>();
for(int i = 0; i < 5; i++){heros.add(new Hero(“hero name” + i));}
for(int i = 0; i < heros.size(); i ++){ Hero h = heros.get(i);}
2 使用迭代器Iterator遍历集合中的元素
Iterator<Hero> it=heros.iterator();
while(it.hasNext()){Hero h = it.next();}
for (Iterator<Hero>iterator = heros.iterator();iterator.hasNext();){
Hero hero = (Hero) iterator.next();}
3 用增强型for循环
for (Hero h : heros){ }
序列分为先进先出FIFO,先进后出FILO
FIFO在Java中又叫做queue队列
FILO在Java中又叫做stack栈
LinkedList与List接口
1 与ArrayList一样,LinkedList 也实现了List接口
2 双向链表Deque(可以方便的在头尾插入删除数据)
链表结构:与数组结构相比,数组结构,就好像是电影院,每个位置都有表示,每个位置之间的间隔都是一样的。而链表就相当于佛珠,每个珠子,只连接前一个和后一个,不用关心除此之外的佛珠在哪里
3 队列queue
Queue 是先进先出队列FIFO,常用方法:
offer在最后添加元素;poll取出第一个元素;peek查看第一个元素
二叉树
概念:二叉树由各种节点组成
特点:每个节点都可以有左子节点,右子节点,每个节点都有一个值
插入数据:插入基本逻辑,小,相同的放左边,大的放右边
二叉树遍历:二叉树其实已经排好顺序,二叉树的遍历分为左序,中序,右序
HASHMAP
HASHMAP储存数据的方式是——键值对;对HashMap而言,key是唯一的,不可以重复的。
所以相同的key把不同的value插入到Map中会导致旧元素被覆盖,只留下最后插入的元素。不过,同一个对象可以作为值插入到map中,只要对应的key不一样
HashSet
Set中的元素,不能重复,没有顺序
Set不提供get()来获取指定位置的元素,所以遍历需要用到迭代器,或者增强型for循环
HashSet自身并没有独立的实现,而是在里面封装了一个Map。HashSet是作为map的key,而value是一个命名为PRESENT的static的object对象,因为是一个类属性,所以只会有一个
collection
collection是Set List Queue和Deque的接口,和Map之间没有关系
collection是放一个一个对象的,map是放键值对的
collections
Collections是一个类,容器的工具类,就如同Array是数组的工具类
reverse使List中的数据发生反转,例如:Collections.reverse(numbers);
shuffle混淆list中数据的顺序,例如:Collections.shuffle(numbers);
sort对list中的数据进行排序,例如:collections.sort(numbers);
swap交换两个数据的位置,例如:Collections.swap(numbers,0,5);
rotate把list中的数据,向右滚动指定单位的长度,例如:Collections.rotate(number,2);
synchronizedList把非线程安全的list转换为线程安全的list
初识mybatis
添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fourth</groupId>
<artifactId>fourthtest</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<name>fourthtest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>
mybatis的依赖包中有很多日志包,既然mybatis依赖log4j,那我们首先要建立一个log4j.properties文件
# Global logging configuration
# developer-->DEBUG productor-->INFO or ERROR
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
mybatis需要配置一个全局配置文件SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 和Spring整合后environment配置都会被干掉 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理,目前由mybatis来管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池,目前由mybatis来管理 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mydatabases?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="sqlmap1/User.xml" />
</mappers>
</configuration>
明天计划的事情:
继续学习 Java基础——集合框架,开始学习Java基础——I/O;配置mybatis并初步进行数据库的操作
遇到的问题:
收获:对Java基础加深,了解mybatis
评论