当前位置:网站首页 > Java基础 > 正文

java黑马程序员基础高级



一、计算机基础知识

Code:
#import <Foundation/Foundation.h>

1、 软件开发:

 *什么是软件?

  软件是一系列按照特定顺序组织的计算机数据和指令的集合。程序设计的最终结果是软件

  系统软件:系统软件为计算机使用提供最基本的功能:如:DOS,Windows,Linux,Unix,Mac,Android,IOS

2、人机交互

  图形化界面:

  命令行方式 

3、Java语言java黑马程序员基础高级特征(跨平台)

Java通过java虚拟机跨平台(JVM)

4、JDK、JRE、JVM之间的关系

  JDK是整个java和核心,包括java的运行环境、java工具、java基础类库

  JRE,运行JAVA程序所必须的环境的集合,保护JVM标准实现及java核心类库

  JVM是java虚拟机,是一种用于计算设备的规范,他是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟平台

  相关信息,使jva程序只需生产在java虚拟机上运行的目标代码,就可以在多种平台上不加修改地运行。

5:JDK安装:

  官网:www.oracle.com  java.sun.com

  在JDK里面就有JRE,所以提示安装JRE时可以不装

6.在我的电脑-熟悉=高级=环境变量设置java工具即bin所在路径就可以了

  注:%%代表调用,需要用;分割

临时配置,在dos中输入set path=你携带的U盘就JRE中的bin的路径

第二讲  基本语法及实例

1、进制的转换

实例:

 1 import java.util.LinkedList;  2  3 import javax.swing.text.html.HTMLDocument.Iterator;
 4  5 //查表法转换进制  6 public class jinZhi {  7 public static void main(String [] args){  8  9 int num=6; 10 11  tobin(num); 12 13  toOct(num); 14 15  toHex(num); 16  } 17 private static void toHex(int num) { 18 con(num,15,4); 19 20  } 21 22 private static void toOct(int num) { 23 con(num,7,3); 24 25  } 26 27 private static void tobin(int num) { 28 con(num,1,1); 29 30  } 31 //列表法结合集合运用实例 32 private static void con(int num, int diwei, int yiwei) { 33 if(num==0){ 34 System.out.println("num="+0); 35 return; 36  } 37 38 // 表格法 39 char[] chs={'0','1','2','3','4','5','6','7', 40 '8','9','a','b','c','d','e','f'}; 41 42 LinkedList<Character> list=new LinkedList<Character>(); 43 44 while(num!=0){ 45 list.addFirst(chs[num&diwei]); 46 num>>>=yiwei; 47  } 48 while(!list.isEmpty()){ 49 System.out.print(list.removeFirst()); 50  } 51 System.out.println(); 52  } 53 }

直接转换的快捷方法:

1 Integer.toHexString(int num); 2 10进制——16进制 3 Integer.toOctalString(int num); 4 10进制——8进制 5 Integer.toBinaryString(int num); 6 10进制——2进制

2.数据类型

注意:String是类不是基本数据类型

  当出现Object数组Object[],可以看出是引用数据类型,是不能放入基本数据类型的;

第三讲  面向对象

概述:

  面向过程强调过程、执行者、强调功能行为;

  面向对象强调功能、指挥者、将功能封装进对象,强调具备功能的对象

面向对象思想的特点:问题简单化,执行者变指挥者

三大特征:封装、继承、多态

1、封装:

  封装就是将具体对象一些属性和方法通过封装打包到一起,对外提供一种访问形式。

2、继承:

  当多个类拥有相同的功能时,那么这些类不需要每个都写这些相同的功能,只需要把相同功能抽到

一个单独的类中,继承这个类就能获得这些相同的功能;

3、多态:

  多态可以理解为事物存在的多种体系形式。比如猫,是猫也是动物 

 

接口:

  当一个类中所有的方法都是抽象的时候,你没必要定义为抽象类,定义为接口就可以了。

(2)成员特点:所以不能被创建,只能被继承

  • A:只有成员变量和成员方法。
  • B:成员变量 默认修饰符 public static final
    • int X = 20;
    • 其实是这样的 public static final int X = 20;
  • C:成员方法 默认修饰符 public abstract
    • void show();
    • 其实是这样的 public abstract void show();
    • 建议:为了便于阅读,自己手动加上修饰符

第四讲  String类和基本数据类型包装类

一、String

1、String方法

  

  int length():获得长度    char charAt(index):获得指定角标的字符返回字节

  

  int indexOf(int ch(ASCII码)):返回ch在字符中第一次出现的位置的角标,没有则返回-1

  int indexOf(int ch,int fromIndex):从fromIndex位置开始,获取ch在字符串中位置

  int indexOf(String Str):获取str的第一次出现位置

  int indexOf(String str,int fromIndex):获取从fromIndex后第一次出现的位置角标

 

  boolean contains(str):判断是否包含___常用

  isEmpty():判断是否空——常用

  startsWith(str):头部有某字符串

  endsWith(str):尾巴有某字符串

  equals:判断字符串是否相同——常用——也经常被复写

  equalsIgnoreCase:忽略大小写的判断字符串是否相同

2、String转换

  构造函数:String(char[])

       String(char[],offset,count)//讲字符数组一部分转换为字符串

3

  char[] tocharArray():将字符串转换为字符数组

  String(byte[]);将字符数组转换为字符串

  String(byte[],offset,count)

  byte[] getBytes();将字符串装好为字符数组 

  String valueOf(int):int变String

  String valueOf(double):double变字符串

  

4、替换:String replace(older,newer)

5切割:String[] split("");切割括号捏的字符串,分成若干字符串组成字符串数组

6、获得字符串的一部分 String subString(begin,end)

7、大写:UpperCase() 小写:toLowerCase()

  compareTo:字符串的自然顺序排序:常用

实例:判断字符串key在字符串Str出现的次数:

 1 public static int count(String str,String key){  2 int count=0;  3 int index=0;  4  5 while( (index=str.indexOf(key,index))!=-1){  6 index=index+key.length();  7 count++;  8  }  9 return count; 10 }

实例:反转字符串数组:

 1 class ReverString{  2  3  4 public static String reverseString(String s){  5 return reverseString(s,0,s.length());  6  }  7  8 public static String reverseString(String s, int start, int end) {  9 // 变字符数组 10 char[] ch=s.toCharArray(); 11 // 将数组反转,已经得到ch的对象的反转字符数组 12  charRecerse(ch,start,end); 13 14 return new String().valueOf(ch); 15  } 16 17 public static void charRecerse(char[] ch, int start, int end) { 18 for(int x=start,y=end-1;x<y;x++,y--){ 19  swap(ch,x,y); 20  } 21  } 22 private static void swap(char[] ch,int x,int y){ 23 char t=ch[x]; 24 ch[x]=ch[y]; 25 ch[y]=t; 26  } 27 }

实例:寻找相同子字符串

 1 /*  2  * 需求:获取连个字符串中最大的相同子串  3 */  4  5 public static String getMaxSubString(String s1,String s2){  6  String max,min;  7 max=(s1.length()>s2.length())?s1:s2;  8 min=(max==s1)?s2:s1;  9 10 for(int x=0;x<min.length();x++){ 11 for(int y=0,z=min.length()-x;z<min.length()+1;y++,z++){ 12 String tenp=min.substring(y,z); 13 if(max.contains(tenp)){ 14 return tenp; 15  } 16  } 17  } 18 return "没有字符串"; 19 20 }

二、StirngBuffer

 1、 一个长度可变的,可以操作多个数据类型的、可以通过toString方法变成字符串的容器

 2方法:

  append():添加

  insert(数据,index):指定位置添加

  delete(start,end)删除

  delexteCharAt(index):删除指定位置

  charAt(index):获取并返回char

  indexOf(Str):返回字符串的位置

  length():长度

  subString(start,end):获取指定位置返回String

  setCharAt(index,char ch):

反转:  reverse()

三、基本数据类型的包装类

  将基本数据类型封装成对象的好处:可以在对象中定义许多功能方法操作该数据

byte---Byte      short———Short    int——Integer   long——Long

boolean——Boolean    float——Float    double——Double    char——Character

包装类的常用方法:

  static int parseInt(String str):将数组字符串转换成数组

  Integer.toBinaryString();10转2

  Integer.toOctalString():10转8

  Integer.toHexSfring():10转16

 

实例:

 1 private static String StringSort(String s) {  2 // 分割  3 String[] str=s.split(" ");  4 // 转成int型  5 int[] arr=new int[str.length];  6 for(int x=0;x<str.length;x++){  7 // 使用Interger.parseInt方法将字符数组变Int数组  8 arr[x]=Integer.parseInt(str[x]);  9  } 10  Arrays.sort(arr); 11 12 StringBuilder sb=new StringBuilder(); 13 for(int x=0;x<arr.length;x++){ 14 sb.append(arr[x]+""); 15  } 16 sb.append(arr[arr.length-1]+""); 17 18 return sb.toString(); 19 }

Arrays工具类:  

  static sort(所有数据类型);


          使用二分搜索法来搜索指定的 byte 型数组,以获得指定的值。
          使用二分搜索法来搜索指定的 byte 型数组的范围,以获得指定的值。
          使用二分搜索法来搜索指定的 char 型数组,以获得指定的值。
          使用二分搜索法来搜索指定的 char 型数组的范围,以获得指定的值。
          使用二分搜索法来搜索指定的 double 型数组,以获得指定的值。
          使用二分搜索法来搜索指定的 double 型数组的范围,以获得指定的值。
          使用二分搜索法来搜索指定的 float 型数组,以获得指定的值。
          使用二分搜索法来搜索指定的 float 型数组的范围,以获得指定的值。
          使用二分搜索法来搜索指定的 int 型数组,以获得指定的值。
          使用二分搜索法来搜索指定的 int 型数组的范围,以获得指定的值。
          使用二分搜索法来搜索指定的 long 型数组的范围,以获得指定的值。
          使用二分搜索法来搜索指定的 long 型数组,以获得指定的值。
          使用二分搜索法来搜索指定数组的范围,以获得指定对象。
          使用二分搜索法来搜索指定数组,以获得指定对象。
          使用二分搜索法来搜索指定的 short 型数组的范围,以获得指定的值。
          使用二分搜索法来搜索指定的 short 型数组,以获得指定的值。

          使用二分搜索法来搜索指定数组的范围,以获得指定对象。

          使用二分搜索法来搜索指定数组,以获得指定对象。

第五讲  多线程

一个进程又多个线程同时操控,从而达到同时运行的效果:

1两种方式

  extend Thread方式:首先类继承Thread,然后复写run;创建这个类,使用start()方法启动

  inplements Runnable:类implements Runnable,复写run;创建类,创建线程Thread一类对象为参数,使用startt(0启动

2、买票练习:

 1 import java.util.Arrays;  2 //买票  3 public class StringSort {  4  5 public static void main(String[] args){  6 Ticket tk = new Ticket();//建立票信息对象  7 sealCentre tsc = new sealCentre(tk);// 创建售票中心。  8 tsc.set(200);//售票中心分配一定数量的票   9 new Thread(new windowsSeal(tk,"一号窗口")).start();// 创建、启动线程,开始卖票。 10 new Thread(new windowsSeal(tk,"二号窗口")).start(); 11 new Thread(new windowsSeal(tk,"三号窗口")).start(); 12 new Thread(new windowsSeal(tk,"四号窗口")).start(); 13  } 14 15 } 16 17 18 class Ticket{ 19 private int ticket; 20 21 public int getTicket() { 22 return ticket; 23  } 24 25 public void setTicket(int ticket) { 26 this.ticket = ticket; 27  } 28 29 30 } 31 32 33 class sealCentre{ 34 private Ticket tk; 35  sealCentre(Ticket tk){ 36 this.tk=tk; 37  } 38 public void set(int t){ 39  tk.setTicket(t); 40  } 41 } 42 43 class windowsSeal implements Runnable{ 44 private Ticket tk; 45 private String name; 46  windowsSeal(Ticket tk,String name){ 47 this.tk=tk; 48 this.name=name; 49  } 50  @Override 51 public void run() { 52 53 while(true){ 54  synchronized (tk) { 55 int t=tk.getTicket(); 56 if(t>0){ 57 System.out.println(name+""+(t--)+"张票卖出"); 58  tk.setTicket(t); 59 }else{ 60 System.out.println(name+"票已经售完"); 61 // System.exit(0); 62 break; 63  } 64  } 65  } 66  } 67 }

 在Java当中,线程通常都有五种状态,创建、就绪、运行、阻塞和死亡。
  第一是创建状态。在生成线程对象,并没有调用该对象的start方法,这是线程处于创建状态。
  第二是就绪状态。当调用了线程对象的start方法之后,该线程就进入了就绪状态,但是此时线程调度程序还没有把该线程设置为当前线程,此时处于就绪状态。在线程运行之后,从等待或者睡眠中回来之后,也会处于就绪状态。
  第三是运行状态。线程调度程序将处于就绪状态的线程设置为当前线程,此时线程就进入了运行状态,开始运行run函数当中的代码。
  第四是阻塞状态。线程正在运行的时候,被暂停,通常是为了等待某个时间的发生(比如说某项资源就绪)之后再继续运行。sleep,join(),wait(),yield等方法都可以导致线程阻塞。
  第五是死亡状态。如果一个线程的run方法执行结束或者调用stop方法后,该线程就会死亡。对于已经死亡的线程,无法再使用start方法令其进入就绪。

线程之间的通信:

1、多个线程并法执行是,CUP是随机切换的,如果我们希望他们有规律的执行,例如:各自运行一次

2、方法步骤:如果希望线程等待wait();希望被唤醒notify;这两个方法必须同步,并且使用同步锁为对象

3、多个线程通信出现问题:

  notify()方法是随机唤醒一个线程

  notifyAll()方法是唤醒所有线程

  JDK5之前无法唤醒指定线程

  如果多个线程之间通信,需要使用notifyAll()通知所有线程,用while反复判断条件

第六讲  集合

主要掌握的集合:

Collection  

    List:

      |--ArrayList:底层的数据结构使用的是数组结构;查询快,增删慢,不同步

      |-LinkedList:底层的数据结构使用的是链式结构;查询慢,增删快

       |-Vector:底层数组结构:同步

    Set:不可重复,无索引,无序()(注意:Set集合都要求不可重复的,所以如何做到不可重复是这里要掌握的)

      |——HashSet:底层数据结构式哈希表。不同步,判断依据是hashCode值,然后是equals

      |——TreeSet:底层数据位2查数,判断依据是compareTo和hashCode

        还有LinkedHashSet

 Map

      |-HashMap:哈希表,hashCode,equals

      |-TreeMap:二叉树,compareTO,hashCode

        还有Hashtable 和LinkedHashMap

实例:使用ArrayLIst添加自定义对象,注意复写equals

 1 mport java.lang.*;  2 2 import java.util.*;  3 3 /*需求:使用ArrayList添加字符串元素,并提出重复项  4  4 * 原理:添加一个临时容器,在迭代原集合,临时集合包含时才添加到临时集合  5  5     由于contains判断依据是equals所以我在自定义的类中复写equals  6  6 */  7 7  8 8 class Student{  9 9 private String name; 10 10 private int age; 11 11 Student(String name,int age){ 12 12 this.name=name; 13 13 this.age=age; 14 14 } 15 15 public String getName() { 16 16 return name; 17 17 } 18 18 19 19 public int getAge() { 20 20 return age; 21 21 } 22 22 public boolean equals(Object obj) { 23 23 if (!(obj instanceof Student)){ 24 24 return false; 25 25 } 26 26 Person p=(Person)obj; 27 27 return this.name.equals(name)&&this.age==age; 28 28 } 29 29 30 30 } 31 31 public class ArrayListDemo { 32 32 public static void main(String[] args){ 33 33 ArrayList<Student> al=new ArrayList<Student>(); 34 34 35 35 al.add(new Student("shuiyin",35)); 36 36 al.add(new Student("shuiyin",25)); 37 37 al.add(new Student("shuiyin",15)); 38 38 al.add(new Student("shuiyin",15)); 39 39 40 40 al=(ArrayList<Student>) zouni(al); 41 41 42 42 Iterator<Student> it=al.iterator(); 43 43 while(it.hasNext()){ 44 44 System.out.println(it.next()); 45 45 } 46 46 47 47 } 48 48 49 49 private static List zouni(ArrayList<Student> list) { 50 50 // 迭代 51 51 Iterator<Student> it=list.iterator(); 52 52 List<Student> al=new ArrayList<Student>(); 53 53 while(it.hasNext()){ 54 54 Object obj=it.next(); 55 55 if(al.contains(obj)){ 56 56 al.add((Student) obj); 57 57 } 58 58 } 59 59 return al; 60 60 } 61 61 }

实例:LinkedList增删方面,尤其是他在头尾添加取出元素的方法好用

 1 模拟堆栈和队列  2  3 1 LinkedList<String> l=new LinkedList();  4 2 l.addFirst("shuiyin01");  5 3 l.addFirst("shuiyin02");  6 4 l.addFirst("shuiyin03");  7 5 l.addFirst("shuiyin04");  8 6 l.addFirst("shuiyin05");  9 7 // 堆栈 10 8 stack(l); 11 9 // 队列 12 10 queque(l); 13 11 } 14 12 15 13 private static void queque(LinkedList<String> l) { 16 14 while(!l.isEmpty()){ 17 15 System.out.print(l.removeLast()); 18 16 } 19 17 20 18 } 21 19 22 20 private static void stack(LinkedList<String> l) { 23 21 while(!l.isEmpty()){ 24 22 System.out.print(l.removeFirst()); 25 23 } 26 24 }

使用HashSet集合注意需要复写HashCode()和equals()方法

 1 public boolean equals(Object obj){  2 16 if(!(obj instanceof Student)){  3 17 return false;  4 18 }  5 19 Student s=(Student)obj;  6 20 return this.name.equals(s.name)&&this.age==age;  7 21 }  8 22  9 23 public int hashCode(){ 10 24 return this.name.hashCode()*31+this.age*2; 11 25 }

是TreeSet就比较麻烦需要implements Comparable 同时复写comparaTo

 1 class Student implements Comparable{  2 7 private String name;  3 8 private int age;  4 9 Student(String name,int age){  5 10 this.name=name;  6 11 this.age=age;  7 12 }  8 13 // 需要复写compareTo()使得比较结构-1,0,1,比较年龄在比较名字compareTo  9 14 public int compareTo(Object obj){ 10 15 if(!(obj instanceof Student)){ 11 16 throw new RuntimeException("类型不对啊!"); 12 17 } 13 18 Student s=(Student)obj; 14 19 if(this.age==s.age){ 15 20 return this.name.compareTo(s.name); 16 21 } 17 22 return new Integer(this.age).compareTo(new Integer(s.age)); 18 23 // 可以return this.age-s.age;上面这样可以保证结果是-1,1 19 24 } 20 25 21 26 public String getName() { 22 27 return name; 23 28 } 24 29 25 30 public int getAge() { 26 31 return age; 27 32 }

TreeSet还有自身独特的优先级高的方法:比较器

 1 TreeSet<Student> al=new TreeSet<Student>(new myCompare());(新创比较器做参数)  2  3  4 //实现Comparator并复写compare(elements1,elements2){}  5 //定义一个比较器哦,以性命的长度作为主要比较  6 20 class myCompare implements Comparator<Student>{  7 21  8 22 public int compare(Student s1, Student s2) {  9 23 int num=s1.getName().length()-s2.getName().length(); 10 24 // 相同是比较CompareTo 11 25 if(num==0){ 12 26 return s1.getName().compareTo(s2.getName()); 13 27 } 14 28 return num; 15 29 }

Map集合掌握keySet和entrySet的取值方法:根据集合底层类型决定自定义类中复写的方法:

如Hash就复写HashCode()和equals;如Tree就复写compareTo;

keySet迭代法:

1 65 private static void keySet(HashMap<Student, String> hm) { 2 66 // 使用keySet迭代法 3 67 Iterator<Student> it=hm.keySet().iterator(); 4 68 5 69 while(it.hasNext()){ 6 70 Student key=it.next();//得到键 7 71 String value=hm.get(key); 8 72 System.out.println(key+"::"+value); 9 73 }

entrySet迭代法:

1 Iterator<Map.Entry<Student,String>> it=hm.entrySet().iterator(); 2 16 while(it.hasNext()){ 3 17 Map.Entry<Student, String> me=it.next(); 4 18 Student key=me.getKey(); 5 19 String value=me.getValue(); 6 20 System.out.println(key+""+value);

扩展:

  BigInteger

1创建:BigInteger(String)来创建一个很大的整数,可以无限大,之创建后不会被改变(类似String)

2.常用方法:

×BigInteger add(BigInter val);加

×BigInteger subtract(BigInter val);减

×BigInteger multiply(BigInter val);乘

×BigInteger divide(BigInter val);除

×BigInteger mod(BigInter val);摸,取余

×BigInteger max(BigInter val);最大值

×BigInteger min(BigInter val);最小值

第七讲  IO流

读写数据用的类:InputStream   OutputStream   Reader    Writer以及他们的子类组成IO流

实例:键盘输入,把输入的数据写入文件,,大写后按over结束;

 1 import java.io.*;  2  3 public class OutInDemo {  4 public static void main(String[] args)throws Exception{  5 BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));  6  7 BufferedWriter bufw=new BufferedWriter(new FileWriter("D:\tt.txt"));  8  9 String line=null; 10 11 while((line=bufr.readLine())!=null){ 12  bufw.write(line); 13  bufw.newLine(); 14  bufw.flush(); 15  } 16 17  bufw.close(); 18  bufr.close(); 19  } 20 }

拷贝媒体文件:

1 BufferedOutputStream bufo=new BufferedOutputStream(new FileOutputStream("aa.mp3")); 2 BufferedInputStream bufi=new BufferedInputStream(new FileInputStream("bb.mps")); 3 4 int len=0; 5 while((len=bufi.read())!=-1){ 6  bufo.write(len); 7  } 8  bufo.close(); 9 bufi.close();

序列流的玩法:

 1 FileInputStream fs1=new FileInputStream("a.txt");  2 FileInputStream fs2=new FileInputStream("b.txt");  3 SequenceInputStream sis=new SequenceInputStream(fs1,fs2);  4  5 FileOutputStream fos=new FileOutputStream("c.txt");  6  7 byte[] bu=new byte[104];  8 int len=0;  9 while((len=sis.read(bu))!=-1){ 10 fos.write(bu,0,len); 11  } 12  sis.close(); 13 fos.close();

整合多个

 1 FileInputStream fs1=new FileInputStream("a.txt");  2 FileInputStream fs2=new FileInputStream("b.txt");  3 FileInputStream fs3=new FileInputStream("c.txt");  4  5 Vector<InputStream> v=new Vector<InputStream>();  6  v.add(fs1);  7  v.add(fs2);  8  v.add(fs3);  9 10 Enumeration<InputStream> en=v.elements(); 11 12 SequenceInputStream sis=new SequenceInputStream(en); 13 14 FileOutputStream fos=new FileOutputStream("c.txt"); 15 16 byte[] bu=new byte[104]; 17 int len=0; 18 while((len=sis.read(bu))!=-1){ 19 fos.write(bu,0,len); 20  } 21  sis.close(); 22 fos.close();

内存输出流:

该输出流可以向内存中希尔数据,把内存当作一个缓冲区,然后一次性获取所有数据

 1 public static void main(String[] args)throws Exception{  2  3 FileInputStream fis=new FileInputStream("a.txt");  4  5 ByteArrayOutputStream bis=new ByteArrayOutputStream();  6 int b;  7 while((b=fis.read())!=-1){  8  bis.write(b);  9  } 10 // 只需要 一次性输出就行了 11 System.out.println(bis); 12 13  fis.close(); 14 }

IO流方法总结
1、字节流:
  ×FileInputStream,FileOutputStream,自定义数组拷贝文件
  ×BufferedInputStream,BufferedOutputStream,内置缓冲区拷贝文件

2、字符流:
  ×FileReader,FileWriter
  ×InoutStreamReader,OutputStreamWriter,转换流,在键盘录入和编码表转换使用过
  ×BufferedReader,BufferedWriter,记得newLine和flush,除非了使用PrintWriter

3、File
  ×掌握文件的递归
    


版权声明


相关文章:

  • 有php基础转java2024-11-12 23:50:05
  • java基础抽象类2024-11-12 23:50:05
  • java基础代码库2024-11-12 23:50:05
  • java看到基础语法2024-11-12 23:50:05
  • java使用mysql基础操作2024-11-12 23:50:05
  • 《java基础入门》源代码2024-11-12 23:50:05
  • java基础入门语法2024-11-12 23:50:05
  • java基础的语法大全2024-11-12 23:50:05
  • 黑马Java32期基础 就业班2024-11-12 23:50:05
  • java基础教程 csdn2024-11-12 23:50:05