java基础知识

自学java基础知识梳理

1. foreach使用

int []arry={10,20,30,50};
        arry[0]=10;
        arry[1]=2;
        for (int i : arry) {
            System.out.println(i);
        }

2. 判断某年某月有多少天

public static void main(String[] args) {
        System.out.println(GetCal());
    }

    /**
     * 计算某年的某月有多少天
     *
     */
  static String GetCal() {
        System.out.println("test2.main()");
        //实例化输入对象
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入年份");
        //获取输入的值
        int year = scanner.nextInt();
        System.out.print("请输入月份");
        int month = scanner.nextInt();
        int day = 0;
        //判断月份
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                day=30;
                break;
                case  2:

                    //计算瑞年
                     if(year%4==0 && year%100!=0 || year%400==0)
                    day=29;
                else
                    day=28;
                break;
               default:
                   System.out.println("月份输入错误");
                   System.exit(0);
                   break;

        }

        return year+"年"+month+"月有"+day+"天";
    }

3. 数组的使用

案例:输入一批数字,计算其总和
  /**
   输入一批数字 
   * 计算其综总和
   */
  static  String Sum(){
      System.out.println("请输入需要求和的数字,以英文逗号分割");
      Scanner scanner=new Scanner(System.in);
      String dataString=scanner.next();
     String[]data= dataString.split(",");
     int sum=0;
      try {
          for(String xString:data){
         if (xString==null||xString.isEmpty()) {
             continue;
         }else{
         sum+= Integer.parseInt(xString);

         }

     } 
      } catch (Exception e) {
          System.out.println("含有无效数字");
          System.exit(0);//发生错误,强制退出
      }
   return  "总和为"+sum ;
  }

4.String类

常用操作
static  void  TestString(){
  String xString="今天心情一点都不美丽";
  //contains() 方法用来检测字符串是否包含某个子串
      if (xString.contains("心")) {
          System.out.println("存在");
      }
    //charAt() 方法的作用是按照索引值获得字符串中的指定字符
      System.out.println(xString.charAt(5));
  //replace() 方法字符串替换,用来替换字符串中所有指定的子串
  //第一个参数指将要被替换的字符串 第二个参数是新的字符串
      System.out.println(xString.replace("一点都不", "很"));
  // split() 方法 分割字符串 上面已经用到
  }

5.StringBuilder 类

常用于频繁使用字符串操作 比如追加
 StringBuilder sb=new StringBuilder();
  sb.append("ss");
  sb.insert(1, "ssss");
 //删除多个字符
  sb.delete(0, 0);
  //删除指定字符
  sb.deleteCharAt(0);

6.类与对象

6.1 定义一个类

/**
 *自定义类
 * @author Administrator
 */
public class PerSon {

    //类字段
    public String Name;
    public  int Age;
    /**
     构造函数
     */
    public PerSon(String name,int age) {
        this.Name=name;
        this.Age=age;
        System.out.print("我是"+Name+",今年"+Age+"岁");
    }

    /**
     类的方法[行为]
     * 公有的
     * 外部可以访问
     */
    public  void  Hellow(){
      System.out.print("我是"+Name+"我今年出生的。【好像不对】");

    }
    /**
     私有方法
     * 外部不可以访问
     */
    private  void  Hellow1(){
     System.out.print("你看不到我");

    }

}
调用类:
        //实例化类
        PerSon perSon=new PerSon("小强", 20);
        //调用类的方法
        perSon.Hellow();
        //访问类的变量
       String nameString= perSon.Name;

7.集合

List集合
  // <editor-fold> 代码折叠
        //创建集合
       Collection<String> list=new  ArrayList<>();
       list.add("a");
        list.add("B");
         list.add("c");
        System.out.println(list.size());
        String bString="";
        //创建迭代器 iterator1.hasNext()判断是否有下一个元素
        for (Iterator<String> iterator1 = list.iterator(); iterator1.hasNext();) {
            String next = iterator1.next();//获取元素 返回object对象
            bString+=next+"_";
        }
        System.out.println(bString);
        // </editor-fold>

        //<editor-fold>   创建List集合
        /**
         Lsit继承Collection拥有其方法
         * Lsit中的两个方法
         * get(int index); 根据索引获取集合的值
         * set(int index,object o);将指定索引的值修改为其他值

         */
        List<Integer> list1=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list1.add(i);//向集合中加入元素
        }

        //遍历list集合 foreach形式
        for(Integer xInteger:list1)
        {
            System.out.println(xInteger);
        }        
        //遍历list集合 for形式
        for(int x=0;x<list1.size();x++){
            //使用get(int index)方法获取值
            System.out.println(list1.get(x));

        }
        //</editor-fold>

8 IO相关

8.1 文件操作
import  java.util.*;
import  java.io.*;


 /**
         文件操作类
         * 构造函数 
         * new File(path);//path代表文件路径
         * new File(string pathname,string child);
         * pathname代表文件的抽象路径名  child代表文件名 形如 new File("D:/code","1.txt")
         * 方法:
         * exists()判断是否存在该文件
         * createNewFile();//创建文件
         * delete()删除文件
         * getAbsolutePath(),//获取文件的绝对路径
         * getParent()//获取文件的父级路径
         * getName()获取文件名
         * isFile()判断是否是文件
         * isDirectory()判断是否为文件夹

         */
       File file=new File("1.txt");
        try {
            if (file.exists()) {
                System.out.println("存在该文件");
                //获取文件名
               String filenameString=file.getName(),
                       absolutepath=file.getAbsolutePath(),//获取文件的绝对路径
                       parentPath=file.getParent()//获取文件的父级路径
                       ;


            }else{
             System.out.println("文件不存在,开始创建");
             file.createNewFile();

            }

        } catch (Exception e) {
            //发生错误 强制退出
            System.exit(0);

        }
8.2 文件输入/输出
/*承接上文*/


                //<editor-fold> 文件写入
                /**
                 创建输出流
                 * new FileOutputStream(File file);
                 * new FileOutputStream(File file,bool append); append 表示是否追加元素,如果追加则设为true

                 */
                FileOutputStream outputStream = new FileOutputStream(file,true);
                //创建输入流

                FileInputStream inputStream = new FileInputStream(file);
                try {

                    String MsgString = "\r\n无言独上西楼,月如钩,寂寞梧桐深院锁清秋,剪不断,理还乱。是离愁,别是一番滋味在心头。";
                    byte[] by = MsgString.getBytes();
                    //在文件中写入信息
                    outputStream.write(by, 0, by.length);

                    //关闭流

                    outputStream.close();

                    //创建byte数组 存储读取的信息
                    byte[]newby=new byte[by.length];
                    //读取文件信息,返回其长度
                    int read=inputStream.read(newby);
                    //展示信息
                    System.out.println(new  String(newby, 0, read));
                  inputStream.close();

                } catch (Exception e) {
//                    outputStream.close();
//                    inputStream.close();
                } finally {

                }

                //</editor-fold>


                /**
                 使用FileWriter 写入文件  参数意义相同
                 */
                FileWriter fw = new FileWriter(file, true);

                fw.write("\r\n枯藤老树昏鸦,小桥流水人家。古道西风瘦马,夕阳西下,断肠人在天涯。");
                fw.close();


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值