自学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="今天心情一点都不美丽";
if (xString.contains("心")) {
System.out.println("存在");
}
System.out.println(xString.charAt(5));
System.out.println(xString.replace("一点都不", "很"));
}
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集合
Collection<String> list=new ArrayList<>();
list.add("a");
list.add("B");
list.add("c");
System.out.println(list.size());
String bString="";
for (Iterator<String> iterator1 = list.iterator(); iterator1.hasNext();) {
String next = iterator1.next();
bString+=next+"_";
}
System.out.println(bString);
/**
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);
}
for(Integer xInteger:list1)
{
System.out.println(xInteger);
}
for(int x=0;x<list1.size();x++){
System.out.println(list1.get(x));
}
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 文件输入/输出
/**
创建输出流
* 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[]newby=new byte[by.length];
int read=inputStream.read(newby);
System.out.println(new String(newby, 0, read));
inputStream.close();
} catch (Exception e) {
} finally {
}
/**
使用FileWriter 写入文件 参数意义相同
*/
FileWriter fw = new FileWriter(file, true);
fw.write("\r\n枯藤老树昏鸦,小桥流水人家。古道西风瘦马,夕阳西下,断肠人在天涯。");
fw.close();