java基础
8大基本类型:boolean、char、byte、short、int、long、float、double、void
引用类型:String、Integer、Double、Float、Boolean、Long、Character、Short、接口、数组
运算符:+、-、*、/、%、++、--
关系运算符:>、<、>=、<=、==、!=
逻辑运算符:&、|、&&、||、!
数组定义:
int[] 名字 = new int[5];
int[] 名字 = new int[]{1,2,3,4,5,6,7};
int[] 名字 = {1,2,3,4,5,6,7,8};
Array常用api

int[] intarray={5,2,9};
Arrays.sort(intarray);
for(int i=0;i<intarray.length;i++){
System.out.println(intarray[i]);
}
String ss=Arrays.toString(intarray);
System.out.println(ss);
int ii=Arrays.binarySearch(intarray,9);
System.out.println(ii);
ArrayList、LinkedList
ArrayList<String> aa=new ArrayList<String>();
aa.add("string1");
aa.add("string1");
aa.add("string1");
for(int i=0;i<aa.size();i++){
System.out.println(aa.get(i));
}
LinkedList<String> cc=new LinkedList<String>();
cc.add("string1");
cc.add("string1");
cc.add("string1");
for(int i=0;i<cc.size();i++){
System.out.println(cc.get(i));
}
- ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
- 对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
- 对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。
HashSet
//一样的元素,只会存一个,去重
HashSet<String> bb=new HashSet<String>();
bb.add("string1");
bb.add("string1");
bb.add("string1");
Iterator<String> ss=bb.iterator();
while(ss.hasNext()){
System.out.println(ss.next());
}
HashMap、LinkedHashMap
HashMap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度。遍历时,取得数据的顺序是完全随机的
HashMap最多只允许一条记录的键为Null;允许多条记录的值为 Null
HashMap不支持线程的同步(即任一时刻可以有多个线程同时写HashMap),可能会导致数据的不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap。
Hashtable与 HashMap类似,它继承自Dictionary类。不同的是:它不允许记录的键或者值为空;它支持线程的同步(即任一时刻只有一个线程能写Hashtable),因此也导致了 Hashtable在写入时会比较慢
LinkedHashMap保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的。也可以在构造时带参数,按照应用次数排序。在遍历的时候会比HashMap慢
HashMap<String,Object> nn=new HashMap<String, Object>();
nn.put("str1",1);
nn.put("str2",2);
nn.put("str3",3);
//第一种遍历方式
Iterator<Map.Entry<String, Object>> iterator = nn.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, Object> next = iterator.next();
System.out.println(next.getKey());
System.out.println(next.getValue());
}
//第二种遍历方式,//推荐//
Set<Map.Entry<String, Object>> entries = nn.entrySet();
for(Map.Entry en:entries)
{
System.out.println(en.getValue());
System.out.println(en.getKey());
}
//第三种遍历方式
Set<String> strings = nn.keySet();
for(String s:strings)
{
System.out.println(s);
System.out.println(nn.get(s));
}
LinkedHashMap<String,Integer> linkhas=new LinkedHashMap<String, Integer>();
linkhas.put("str1",1);
linkhas.put("str2",2);
linkhas.put("str3",3);
Iterator<Map.Entry<String, Integer>> iterator = linkhas.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> next = iterator.next();
System.out.println(next.getKey());
System.out.println(next.getValue());
}
Collections
ArrayList<Integer> intarrlist=new ArrayList<Integer>();
intarrlist.add(2);
intarrlist.add(5);
intarrlist.add(1);
intarrlist.add(0);
Collections.sort(intarrlist);
for(int n=0;n<intarrlist.size();n++)
{
System.out.println(intarrlist.get(n));
}
对象:确实存在的个体,是由类产生出来的
类:描述事物的共同属性、方法的一个抽象说明。
类的定义:
public class Student {
//属性//成员变量
int id;
String name;
boolean sex;
int age;
String addr;
//方法
public void fun()
{
System.out.println(name);
int mm;//局部变量
}
}
成员变量与局部变量:
修饰符:
private只能在本类中访问
protected
public
default
面向对象的三大特征:
封装、继承、多态
封装:
1、 属性私有化
2、 对外提供set\get方法
3、 set/get方法遵循规范
public class Man {
private int age;//属性,成员变量
public int getAge()
{
return age;
}
public void setAge(int _age)
{
if (_age > 100||_age<0) {
System.out.println("赋值不合理");
return;
}
age=_age;
}
}
继承:extends,单继承
接口:
1、 接口也是一种引用类型,可以同等看做类
接口语法:[修饰符] interface 接口名()
2、 接口中只能出现常量和抽象方法,接口中的权限固定是public
3、 接口其实是一个特殊的抽象类,特殊在接口是完全抽象的
4、 接口中没有构造方法,接口也无法实例化
5、 接口和接口之间可以多继承
6、 一个类可以实现多个接口
7、 一个非抽象的类实现接口,需要将接口中所有的方法实现、重写
public interface Myface {
//常量
public static final String SuccessStr="mmm";
public static final double PI=3.14;
byte NAM_MAX=127;
//接口中的所有抽象方法都是public abstract
public abstract void s1();
}
interface M1{
void m1();
}
interface M2{
void m2();
}
interface M3 extends M1,M2{
void m3();
}
//implements是实现接口
//implements和extends意义相同,但是extends已经写死了只能单继承;所以引入implements
class Myclass implements M1,M2{
public void m1(){}
public void m2(){}
}
接口的作用:
1、 可以使项目分离,所有层面向接口开发,提高开发效率
2、 接口可以使代码和代码之间的耦合度降低
3、 接口和抽象类都能够完成某个功能的时候,优先选择接口,因为接口可以多实现、多继承,并且一个类除了接口之外,还可以去继承其他类(保留了类的继承)
IO流
字符流
InputStreamReader
FileReader
BufferedReader
OutputStreamWriter
FileWriter
BufferedWriter
FileReader fr=new FileReader("G:\\JavaProject\\testcase1\\src\\main\\resources\\a.txt");
int num;
while((num=fr.read())!=-1)
{
System.out.println(num);
}
fr.close();
FileWriter fw=null;
try {
fw=new FileWriter("G:\\JavaProject\\testcase1\\src\\main\\resources\\a_copy.txt",true);
fw.write("nnnreadew");
fw.write("你好");
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fw.close();
}
catch (IOException e){
e.printStackTrace();
}
}
BufferedReader bf=new BufferedReader(new FileReader("G:\\JavaProject\\testcase1\\src\\main\\resources\\a.txt"));
String data=null;
while ((data=bf.readLine())!=null)
{
System.out.println(data);
}
BufferedWriter bfw=new BufferedWriter(new FileWriter("G:\\JavaProject\\testcase1\\src\\main\\resources\\a_copy.txt",true));
bfw.write("kkkkk");
bfw.newLine();
bfw.write("kkkkkkkkkkkk");
bfw.flush();//如果在数据流大的时候如果直接close有可能造成卡死、丢数据;建议写一部分flush刷新
字节流
FileInputStream
BufferedInputStream
FileOutputStream
BufferedOutputStream
FileInputStream bf=new FileInputStream("G:\\JavaProject\\testcase1\\src\\main\\resources\\a.txt");
int data;
while ((data=bf.read())!=-1)
{
System.out.println((char)data);
}
FileOutputStream bf=new FileOutputStream("G:\\JavaProject\\testcase1\\src\\main\\resources\\a_copy.txt",true);
bf.write("你好".getBytes());
bf.close();
BufferedInputStream bf=new BufferedInputStream(new FileInputStream("G:\\JavaProject\\testcase1\\src\\main\\resources\\a.txt"));
byte[] byt=new byte[1024*1];
int num=0;
while ((num=bf.read(byt))!=-1)
{
System.out.println(new String(byt,0,num));
}
BufferedOutputStream bf=new BufferedOutputStream(new FileOutputStream("G:\\JavaProject\\testcase1\\src\\main\\resources\\a_copy.txt"));
bf.write("哈哈哈哈呵呵呵呵呵".getBytes());
bf.flush();
bf.close();
写Excel
//创建一个工作薄
HSSFWorkbook wb=new HSSFWorkbook();
//创建一个sheet
HSSFSheet sh=wb.createSheet("first");
//创建行
HSSFRow row=sh.createRow(0);
//写入数据
row.createCell(0).setCellValue("你好");
row.createCell(1).setCellValue(28);
row.createCell(2).setCellValue(true);
//写入小数
HSSFCell cell=row.createCell(3);
cell.setCellValue(6000);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
cell.setCellStyle(cellStyle);
//写入日期
HSSFCell celldate = row.createCell(4);
HSSFCreationHelper creationHelper = wb.getCreationHelper();
HSSFCellStyle cellStyledate = wb.createCellStyle();
cellStyledate.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-mm-dd"));
celldate.setCellValue(new Date());
celldate.setCellStyle(cellStyledate);
FileOutputStream outf=null;
try {
outf=new FileOutputStream("G:\\JavaProject\\testcase1\\src\\main\\resources\\myboot.xls");
wb.write(outf);
outf.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
读Excel
InputStream istr=new FileInputStream("G:\\JavaProject\\testcase1\\src\\main\\resources\\myboot.xls");
POIFSFileSystem fs=new POIFSFileSystem(istr);
HSSFWorkbook wb=new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
if(sheet!=null)
{
for(int m=0;m<=sheet.getLastRowNum();m++)
{
HSSFRow row=sheet.getRow(m);
if(row!=null) {
for (int r = 0; r <= row.getLastCellNum(); r++) {
HSSFCell cell = row.getCell(r);
if (cell != null) {
String str ="";
if(cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC) {
if(HSSFDateUtil.isCellDateFormatted(cell))
{
SimpleDateFormat sim=new SimpleDateFormat("yyyy-mm-dd");
str =sim.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
}
else {
str = String.valueOf(cell.getNumericCellValue());
}
}
else if(cell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN)
{
str=String.valueOf(cell.getBooleanCellValue());
}
else {
str = cell.getStringCellValue();
}
if(r==2)
{
str=str.toUpperCase();
}
if(r==1)
{
str=str.substring(0,str.indexOf("."));
}
System.out.println(str);
}
}
}
}
}
Xunit
Junit/TestNG
注解
TestSuite
TestCase
TestRunner
TestResult
TestListener
suite>test>group>class>method
常用断言
assertTrue
assertSame
asserEquals
浙公网安备 33010602011771号