泛型的出现避免了因为类型不同进行强制类型转换报错的情况 通常有:【泛型类】、【泛型接口】、【泛型方法】 泛型标记符:E、T、K、V、N、?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TestT<T> {
private final T data;
public T getData() {
return data;
}
public TestT(T data) {
this.data = data;
}
public static <E> void printArray(E[] inputArray) {
for (E element : inputArray) {
System.out.printf("%s ", element);
}
System.out.println();
}
public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
T max = x;
if (y.compareTo(max) > 0) {
max = y;
}
if (z.compareTo(max) > 0) {
max = z;
}
return max;
}
public static void getNumber(Number data) {
System.out.println("Number : " + data);
}
public static void getData(List<?> data) {
System.out.println("getData :" + data.get(0) + "\n");
}
public static void getUpNumber(List<? extends Number> data) {
System.out.println("getUpNumber :" + data.get(0) + "\n");
}
public static void main(String args[]) {
Integer[] intArray = {1, 2, 3, 4, 5};
Double[] doubleArray = {1.1, 2.2, 3.3, 4.4};
Character[] CharacterArray = {'H', 'E', 'L', 'L', 'O'};
String[] stringArray = {"test", "element", "super", "title"};
System.out.println("整型数组元素为:");
printArray(intArray);
System.out.println("\n双精度型数组元素为:");
printArray(doubleArray);
System.out.println("\n字符型数组元素为:");
printArray(CharacterArray);
System.out.println("\n字符型数组元素为:");
printArray(stringArray);
System.out.println("\n======================================\n");
System.out.printf("%d, %d 和 %d 中最大的数为 %d\n\n", 3, 4, 5, maximum(3, 4, 5));
System.out.printf("%.1f, %.1f 和 %.1f 中最大的数为 %.1f\n\n", 6.6, 8.8, 7.7, maximum(6.6, 8.8, 7.7));
System.out.printf("%s, %s 和 %s 中最大的数为 %s\n", "pear", "apple", "orange", maximum("pear", "apple", "orange"));
System.out.println("\n======================================\n");
Integer data = 100;
getNumber(data);
System.out.println("\n======================================\n");
List<String> name = new ArrayList<>();
List<Integer> age = new ArrayList<>();
List<Number> number = new ArrayList<>();
name.add("test");
age.add(18);
number.add(365);
getData(name);
getData(age);
getData(number);
getUpNumber(age);
getUpNumber(number);
System.out.println("\n======================================\n");
TestT<Integer> integerTestT = new TestT<>(1);
System.out.println(integerTestT.getData());
TestT<String> stringTestT = new TestT<>("test-string");
System.out.println(stringTestT.getData());
TestT<List<String>> listTestT = new TestT<>(Arrays.asList("test", "list"));
System.out.println(listTestT.getData());
}
}
java的io(字节流、字符流)
不同的编码格式占字节数是不同的,UTF-8编码中一个中文所占字节也是不确定的,可能是2个、3个、4个字节; 编码格式:UTF-8、GBK、ASCII 码、UTF-16、GB18030、ISO-8859-1、GB2312等等 idea中的编码格式:通常使用UTF-8
import java.io.*;
import java.nio.charset.StandardCharsets;
public class TestIO {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("test.txt");
OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
writer.append("中文输入:");
writer.append("\n");
writer.append("English:");
writer.close();
os.close();
InputStream is = new FileInputStream("test.txt");
InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
StringBuilder stringBuilder = new StringBuilder();
while (reader.ready()) {
stringBuilder.append((char) reader.read());
}
System.out.println(stringBuilder);
reader.close();
is.close();
}
}
|