• java复习笔记


    本笔记(无异常处理与网络编程部分)整理自《java程序设计》-黄岚 王岩 王康平 编著

    java数据     UI     I/O      java线程      数据库操作

     

    Java数据

     

      byte 8bit

      short 16bit

      int 32bit

      long 64bit

      float 32bit

      double 64bit 

    Integer类

    构造方法:public Integer(int value)
      public Integer(String s)

    提供的方法:public int compareTo(Integer anotherInteger)
      public static int parseInt(String s)
        throws NumberFormatException
      public static int parseInt(String s, int radix)
        throws NumberFormatException
      public static String toBinaryString(int i)
      public static String toString(int i)
      public boolean equals(Object obj)
      public static Integer valueof(int i)
      public int intValue()

    Float类的构造方法:
      public Float(float value)
      public Float(double value)
      public Float(String s)

    常用的成员方法
      public static float parseFloat(String s)
        throws NumberFromatException
      public String toString()
      public String toString(float f)
      public int compareTo(Float anotherFloat)
      public short shortValue()
      public int intValue()

    Character构造方法:
      public Character(char value)
    常用的成员方法
      public static boolean isLetter(char ch)
      public static boolean isDigit(char ch)
      public static char toLowerCase(char ch)
      public static boolean isWhitespace(char ch)
      public static char toUpperCase(char ch)

    Boolean构造方法:public Boolean(boolean value)
      publie Boolean(String s)
    常用的成员方法
      public static boolean parseBoolean(String s)
      public static String toString(boolean b)

    String类

    类String提供了大量的方法,使得字符串的处理更加方便。
      public char charAt(int index) //返回指定索引处的char值
      public int length()//返回此字符串的长度
      public int indexOf(String str) //返回指定字符在第一次出现处的索引
      public boolean equalsIgnoreCase(String another) //忽略大小写的相等
      public String replace(char oldChar,char newChar) //替换所有匹配字符

    提供的方法:
      public boolean startsWith(String prefix) //是否开始于一个字符串
      public boolean endsWith(String suffix) //是否结束于一个字符串
      public String toUpperCase() //转化成大写
      public String toLowerCase() //转化成小写
      public String substring(int beginlndex) //开始于begin位置的子字符串
      public String substring(int beginlndex,int endlndex) //开始于begin位置,结束于end位置的子字符串
      public String trim()//去掉字符串两端的空格

    StringBuilder中常用的方法:
      public StringBuilder append (...) //追加参数(多种类型)中的内容到字符串中
      public StringBuilder insert ( ... ) //将任意参数的字符串形式插入到原有字符串指定的位置
      public StringBuilder delete (int start, int end) //删除从start开始到end-1为止的一段字符序列
      public StringBuilder reverse (StringBuilder str) //将字符序列逆序

    正则表达式*(与字符串紧密相关)

    A example of Regular expressions :
      import java.util.regex.*;
      public class Email {
      public static void main(String[ ] args) throws Exception {
      String input0 = "www.kevin@163.net";
      String input = input0;
      Pattern p = Pattern.compile("^\.|^\@");
      Matcher m = p.matcher(input);
      if (m.find()){
      System.err.println ("EMAIL can not start by '.' or '@'");
      }
      p = Pattern.compile("^www\.");
      m = p.matcher(input);
      if (m.find()) {
      System.out.println("EMAIL can not start by 'www.'");
      }

      p = Pattern.compile("[^A-Za-z0-9\.\@_\-~#]+");
      m = p.matcher(input);
      StringBuffer sb = new StringBuffer();
      boolean result = m.find();
      boolean deletedIllegalChars = false;
      while(result) {
      deletedIllegalChars = true;
      m.appendReplacement(sb, "");
      result = m.find();
      }
      m.appendTail(sb);
      input = sb.toString();
      if (deletedIllegalChars) {
      System.out.println(“Now your input is : "+input0);
      System.out.println(“The correct input need as : "+input);
      }
      }
      }

    图形化界面(GUI)

    容器类
      JFrame、JApplet、JDialog、JPanel
    组件类
      JButton, JTextField, JTextArea, JComboBox, JList, JRadioButton,
      JMenu
    辅助类
      Graphics, Color, Font, FontMetrics, Dimension, LayoutManager

    JFrame构造方法:public JFrame()

      public JFrame(String title)

    成员方法:setSise()

      setVisible()  

      setLocation()

      setDefaultCloseOperation()

      getContentPane()

      setTitle()

      public void setDefaultCloseOperation(int operation)中参数的4种取值:DO_NOTHING_ON_CLOSE,HIDE_ON_CLOSE,DISPOSE_ON_CLOSE,EXIT_ON_CLOSE

    JButton构造方法:public JButton()

      public JButton(String text)

      public JButton(Icon icon)

      public JButton(String text,Icon icon)

    成员方法:

      public void setText(String text)

      public setIcon(Icon icon)

      public void setBounds(int height,int width,int x,int y)

      public void addActionListener(ActionListener I)

    JLabel构造方法:public JLabel(String text)

      public JLabel(Icon icon)

      public JLabel(String text,int horizontalAlignment)

      public JLabel(Icon icon,int horizontalAlignment)

      public JLabel(String text,Icon icon,int horizontalAlignment)

    成员方法:

      public void setText(String text)

      public void setIcon(Icon icon)

      public void getText(String text)

      public void setHorizontalTextPosition(int textPosition)

      public void setVerticalTextPosition(int textPosition)

    JTextField构造方法:public JTextField()

      public JTextField(String text)

      public JTextField(int columns)

      public JTextField(String text,int columns)

    成员方法:

      public void addActionListener(ActionLister I)

      public void setColums(int colums)

      public void setText(String text)

    文本⾯面板JTextPane

    复选框JCheckBox

    单选按钮JRadioButton

    组合框(下拉列表)JComboBox

    绘图Drawing
       继承JPanel

       重写paintComponent(Graphics g)
      Swing 传递一个图形对象给这个方法。
      常用绘图方法
      setColor()
      drawLine()

    对话框JDialog

    文件对话框JFileChooser
       打开文件showOpenDialog()
       存储文件showSaveDialog()

    文本域JTextArea

    菜单:JMenuBar
      JMenu
      JMenuItem

    布局管理:BorderLayout,FlowLayout,GridLayout,GridBagLayout,BoxLayout

    //方法1:直接实现监听器方式
      public class ButtonAct extends Frame implements ActionListener {
      //主类ButtonAct直接实现监听器接口
      private JButton b1;
      ... ... ...
      public ButtonAct() {
      ... ... ...
      b1.addActionListener(this); //为b1注册事件监听器ButtonAct类对象
      add(b1);
      ... ... ...
      }
      public void actionPerformed(ActionEvent e){
      //利用actionPerformed方法进行事件处理
      who.setText("Button 1");
      }
      ... ... ...
      }

    //方法2:内部类实现监听器方式
      public class ButtonAct extends Frame {
      private JButton b1;
      ... ... ...
      public ButtonAct() {
      ... ... ...
      b1.addActionListener(new B1()); //为b1注册事件监听器B1类对象
      add(b1);
      ... ... ...
      }
      class B1 implements ActionListener { //利用内部类B1实现监听器接口
      //利用actionPerformed方法进行事件处理
      public void actionPerformed(ActionEvent e){
      who.setText("Button 1");
      }
      }
      ... ... ...
      }

    //方法3:匿名内部类实现监听器方式
      public class ButtonAct extends Frame {
      private JButton b1;
      ... ... ...
      public ButtonAct() {
      ... ... ...
      b1.addActionListener( //为b1注册事件监听器
      new ActionListener (){ //利用匿名内部类定义
      监听器类
      public void actionPerformed(ActionEvent e){
      //利用actionPerformed方法进行事件处理
      who.setText("Button 1");
      }
      }
      );
      add(b1);
      ... ... ...
      }
      ... ... ...
      }

    java.awt.event包中定义的事件适配器类包括以下
    7个:
      ComponentAdapter(组件适配器);
      ContainerAdapter(容器适配器);
      FocusAdapter(焦点适配器);
      KeyAdapter(键盘适配器);
      MouseAdapter(⿏鼠标适配器);
      MouseMoQonAdapter(⿏鼠标运动适配器);
      WindowAdapter(窗⼝口适配器)。

    I/O流 

    InputStream:FileInputStream

        PipedInputStream//产生用于写入相关PipedOutputStream的数据,实现管道化

        FilterInputStream->LineNumberInputStream,DataInputStream,BufferedInputStream,PushbackInputStream;

        ByteArrayInputStream//允许将内存缓冲区当做InputStream

        SequenceInputStream//将多个InputStream转化为一个InputStream

        StringBufferInputStream//将String转换成InputStream

        ObjectInputStream

    Reader:BufferedReader->LineNumberReader

        CharArrayReader

        InputStreamReader->FileReader

        FilterReader->PushbackReader

        PipedReader

        StringReader

    Writer:同上+PrintWriter

        BufferedWriter有readLine();

    Reader的方法:in read(),int read(char[] cbuf),int read(char[] cbuff,int offset,int length),void close(),long skip(long);

    Writer的方法:void write(int c),void write(char[] cbuf),void write(char cbuf,int offset,int length),void write(String s),void write(String s,int offset,int length),void close(),void flush();

    缓冲流

    没有缓冲的I/O,直接读写效率低,对硬盘损坏大。

      BufferedReader(Reader in)

      BufferedReader(Reader in,int sz)

      BufferedWriter(Write out )

      BufferedWriter(Writer in,int sz)

      BufferedInputStream(InputStream in)

      BufferedInputStream(InputStream in,int sz)

      BufferedOutputStream(OutputStream)

      BufferedOutputStream(OutputStream out,int sz)

    方法:

      void mark();//标记流中当前位置。

      void reset();//尝试将流定位到最近标记的点

      void readLine();//仅适用于BufferedReader

      void newLine;//仅适用于BufferedWriter,写入一个行分隔符

      void flush();//刷新流

      PrintStream (字符)
      PrintWriter (字节)
      为其他输出流添加了功能,使它们能够方便地打印各
      种数据值表示形式。分别针对字节和字符,提供了重
      载的print()和println()方法用于多种数据类型的输出
      不抛出IOException
      自动的flush()

      PrintWriter(Writer out)
      PrintWriter(File file)
      PrintWriter(OutputStream out)
      PrintWriter(String fileName)
      PrintStream(OutputStream out)
      PrintStream(File file)
       PrintStream(String fileName)

    File类 java.io.File

      public File(String pathname)

      public File(String parent,String child)

      public boolean canRead()

      public booleam canWrite()

      public boolean exists()

      public boolean isDirectory()

      public boolean isFile()

      public boolean isHidden()

      public long last Modified()

      public long lengrh()

      public String getName()

      public String getPath()

      public boolean createNewFile() throws IOException

      public boolean delete()

      public boolean mkdir()

      public boolean mkdirs()

    RandomAccessFile(File,String mode)

    RandomAccessFile(String name,String mode)

      void seek(long pos)

      long getFilePointer()

      long length()

      void writeDouble(Double d)

      void writeUTF(String s)

    压缩类:ZipOutputStream,GZipOutputStream,ZipInputStream,GZipInputStream

    GZIP例:  BufferedReader in = new BufferedReader(
          new FileReader(args[0]));
          BufferedOutputStream out = new BufferedOutputStream(
          new GZIPOutputStream(new FileOutputStream("test.gz")));

    线程:Thread 

    构造方法:public Thread(ThreadGroup group,Runnable target,String name)

      public Thread()

      public Thread(ThreadGroup group,Runnable target,String name,long stackSize)

      public Thread(Runnale target)

      public Thread(String name)

      public Thread(Runnable target,String name)

      public Thread(ThreadGroup group,Runnable target)

      public Thread(ThreadGroup group,String name)

    成员方法:interrupt()

      join()//等待该线程终止

      join(long millis,int nanos)

      sleep(long millis)

      sleep(long millis,int nanos)//毫秒+纳秒

      start()//启动线程

      yield()//暂停当前线程,并执行其他进程

      notify()//唤醒在此对象监视器上等待的单个线程

      run()//执行线程体

      notifyAll()

      wait()//导致当前线程等待

      wait(long timeout)//导致当前线程等待timeout毫秒

      wait(long timeout,int nanos)

      isAlive()//测试线程是否处在活动状态

      isInterrupted()

      getPriority()

      getId()

      getName()

      getState()

      setPriority(int newPriority)

      setName(String name)//改线程名字  

      继承Thread类创建线程的具体步骤:

      第1步:扩展Thread的类;
      第2步:用希望的执行代码来实现run()方法;
      public class <类名称> extends Thread {
      public void run() {
      //需要以线程方式运行的代码
      }
      }
      第3步:通过new关键字实例化该类的一个新对象(即一个线程);
      new <类名称>()
      第4步:通过调用start()方法启动线程。

      实现Runable接口创建线程具体步骤:

      第1步;实现Runable接口;

      第2步:用希望的执行代码来实现run()方法; public class <类名称> implements Runable { public void run() { //需要以线程方式运行的代码 } }

      第3步:通过new关键字实例化该类的一个新对象(即一个Runable对象),然后将之用作Thread()构造方法的参数,用来生成新的线程体对象; new Thread (new <类名称>())

      第4步:通过调用start()方法启动线程。

      设置方法同步 synchronized ReturnType methodName(parameterList){ method statement; }
      设置语句块同步 synchronized (object){ statement; }

    数据库操作:

    SQL语言的命令一般分为四类:
      查询语言:select…from…where…
      操纵语言:insert、update、delete…
      定义语言:create、alter、drop
      控制语言:grant、revoke、commit、rollback…

      create table <表名> (字段1 类型1(长度),字段2 类型2(长度) …… )

      drop table <表名>

      select<输出结果列表>from<表>[where<选择条件>][order by<排序条件>][group by<分组条件>]

      insert into<表名>[(<列表名>)]VALUES(<对应列的表值>)
      update<表名>set<列>=<值>[,<列>=<值>][where<定位条件>]
      delete from<表名>[where<条件>]

    JDBC的基本功能:
      ①加载JDBC驱动程序;
      ②建立与数据库的连接;
      ③使用SQL语句进行数据库操作并处理结果;
      ④关闭相关连接。

    JDBC API接口:java.sql.DriverManager  getDriver(String url),getDrivers(),getConnection(),registerDriver(java.sql.Driver driver),setCatalog(String database);

        java.sql.Connection  createStatement(),setAutoCommit(Boolean autoCommit),getAutoCommit(),commit(),close;

        java.sql.Statement  executeQuery(String sql),executeUpdate(String sql),execute(String sql);

        java.sql.ResultSet  next(),previous(),first(),last(),getXxx(id),getXxx("name"),close();

    JDBC编程步骤:

      ①导入java.sql.*包:import
      ②加载JDBC驱动程序:Class.forName()
      ③定义数据库的URL:String url=“jdbc:derby:helloDB "
      ④连接数据库:getConnection(url)
      ⑤建立SQL语句对象:createStatement()
      ⑥执行SQL语句:executeQuery() 、execute() 、executeUpdate()
      ⑦处理结果集:next()、 previous()……
      ⑧关闭连接:commit()、 close()

    java.sql.DriverManager接口URL列表
      Connection cn;
      MySQL:Class.forName( "org.gjt.mm.mysql.Driver" );
      cn = DriverManager.getConnection( "jdbc:mysql://MyDbComputerNameOrIP:3306/myDatabaseName", sUsr, sPwd );
      PostgreSQL(http: Class.forName( "org.postgresql.Driver" );
      cn = DriverManager.getConnection( "jdbc:postgresql://MyDbComputerNameOrIP/myDatabaseName", sUsr, sPwd );
      Oracle:Class.forName( "oracle.jdbc.driver.OracleDriver" );
      cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
      Sybase:Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );
      cn = DriverManager.getConnection( "jdbc:sybase:Tds:MyDbComputerNameOrIP:2638", sUsr, sPwd );
      Microsoft SQLServer:Class.forName( "net.sourceforge.jtds.jdbc.Driver" );
      cn = DriverManager.getConnection( "jdbc:jtds:sqlserver://MyDbComputerNameOrIP:1433/master", sUsr, sPwd );
      Java DB: Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
      cn = DriverManager.getConnection("jdbc:derby:helloDB;create=true", sUsr, sPwd);
      ODBC:Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
      cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd );
      DB2:Class.forName("com.ibm.db2.jdbc.net.DB2Driver");
      cn = DriverManager.getConnection("jdbc:db2://192.9.200.108:6789/SAMPLE", sUsr, sPwd );

  • 相关阅读:
    react-custom-scrollbars的使用
    【react】Mobx总结以及mobx和redux区别
    【React】Redux入门 & store体验
    chrome安装react-devtools开发工具
    【vue】vuex防止数据刷新数据刷掉
    搭建博客的两个工具区别
    JavaScript中的作用域
    通过JavaScript创建表格
    JavaScript中的普通for循环和 for in循环
    JavaScript中创建默认对象的方式
  • 原文地址:https://www.cnblogs.com/yuanzhenliu/p/4904047.html
Copyright © 2020-2023  润新知