day4 java2
public class StringDemo{
public static void main(String args[]){
char[] helloArray = { 'r', 'u', 'n', 'o', 'o', 'b'};
String helloString = new String(helloArray);
System.out.println( helloString );
}
}
注意:String 类是不可改变的,所以你一旦创建了 String 对象,那它的值就无法改变了(详看笔记部分解析)。
如果需要对字符串做很多修改,那么应该选择使用 StringBuffer & StringBuilder 类。
string1.concat(string2);
连接两个字符串
格式化字符串基本同c
https://www.runoob.com/java/java-string.html //一些string函数
public class Test{
public static void main(String args[]){
StringBuffer sBuffer = new StringBuffer("菜鸟教程官网:");
sBuffer.append("www");
sBuffer.append(".runoob");
sBuffer.append(".com");
System.out.println(sBuffer);
}
}
数组
dataType[] arrayRefVar; // 首选的方法
或
dataType arrayRefVar[]; // 效果相同,但不是首选方法
Java语言使用new操作符来创建数组,语法如下:
arrayRefVar = new dataType[arraySize]; // double[] myList = new double[size];
String s[][]= new String[2][]; //多维数组
Date相关
https://www.runoob.com/java/java-date-time.html
正则
https://www.runoob.com/java/java-regular-expressions.html
import java.util.regex.*;
class RegexExample1{
public static void main(String args[]){
String content = "I am noob " +
"from runoob.com.";
String pattern = ".*runoob.*";
boolean isMatch = Pattern.matches(pattern, content);
System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
}
}
public class VarargsDemo {
public static void main(String args[]) {
// 调用可变参数的方法
printMax(34, 3, 3, 2, 56.5);
printMax(new double[]{1, 2, 3});
}
public static void printMax( double... numbers) {
if (numbers.length == 0) {
System.out.println("No argument passed");
return;
}
double result = numbers[0];
for (int i = 1; i < numbers.length; i++){
if (numbers[i] > result) {
result = numbers[i];
}
}
System.out.println("The max value is " + result);
}
}
Java 流(Stream)、文件(File)和IO
//使用 BufferedReader 在控制台读取字符
import java.io.*;
public class BRRead {
public static void main(String args[]) throws IOException {
char c;
// 使用 System.in 创建 BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入字符, 按下 'q' 键退出。");
// 读取字符
do {
c = (char) br.read();
System.out.println(c);
} while (c != 'q');
}
}
Read()读入字符 readline()读入字符串
还可以用write
InputStream f = new FileInputStream("C:/java/hello");
或
File f = new File("C:/java/hello"); InputStream out = new FileInputStream(f);
OutputStream f = new FileOutputStream("C:/java/hello")
或
File f = new File("C:/java/hello"); OutputStream f = new FileOutputStream(f);
InputStream is = new FileInputStream("test.txt");
int size = is.available();
Try
catch判断错误
Scanner 类
Java5新特性
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// next方式接收字符串
System.out.println("next方式接收:");
// 判断是否还有输入
if (scan.hasNext()) {
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
Java异常处理
https://www.runoob.com/java/java-exceptions.html
try
{
// 程序代码
}catch(ExceptionName e1)
{
//Catch 块
}