int arr[]; int[] arr2;
int arr[] = new int[]{1, 3, 5, 7, 9};
int[] arr2 = {2, 4, 6, 8, 10};
public static void main(String[] args) {
int arr[] = new int[]{1, 3, 5, 7 ,9};
int[] arr2 = {2, 4, 6, 8, 10};
for (int i = 0; i < arr.length; ++i) {
System.out.print(arr[i] + "\t"); // 1 3 5 7 9
}
for (int x: arr2) {
System.out.print(x + "\t"); // 2 4 6 8 10
}
}
int[] arr3 = new int[5];
for (int x: arr3) {
System.out.print(x + "\t"); // 0 0 0 0 0 全部初始化为0
}
System.out.println();
Arrays.fill(arr3, 10);
for (int x: arr3) {
System.out.print(x + "\t"); // 10 10 10 10 10 全部填充为10
}
System.out.println();
Arrays.fill(arr3, 1, 3, 8);
for (int x: arr3) {
System.out.print(x + "\t"); // 10 8 8 10 10 填充指定索引
}
System.out.println();
int[] arr4 = {3, 7, 2, 1, 9};
Arrays.sort(arr4);
for (int x: arr4) {
System.out.print(x + "\t"); // 1 2 3 7 9
}
System.out.println();
int[] arr5 = {3, 7, 2, 1, 9};
Arrays.sort(arr5, 1, 3);
for (int x: arr5) {
System.out.print(x + "\t"); // 3 2 7 1 9
}
System.out.println();
int[] arr6 = {1, 2, 3, 4, 5};
int[] arr7 = Arrays.copyOf(arr6, 5); // 1 2 3 4 5
int[] arr8 = Arrays.copyOfRange(arr6, 1, 3); // 2 3
for (int x: arr7) {
System.out.print(x + "\t");
}
System.out.println();
for (int x: arr8) {
System.out.print(x + "\t");
}
System.out.println();
String s = new String("Hello");
String s2 = new String("World");
System.out.println(s + " " + s2); // Hello World
String s3 = new String("Hello Java");
System.out.println(s3.length()); // 10
String s4 = new String("how are you");
System.out.println(s4.indexOf("o")); // 1 从头开始查找
System.out.println(s4.lastIndexOf("o")); // 9 从尾开始查找
String s5 = new String("Hello Java");
System.out.println(s5.charAt(4)); // o
String s6 = new String(" Hello Java ");
String s7 = s6.trim(); // 去除字符串开始、结尾处的空格
String s8 = s6.replaceAll(" ", ""); // 替换字符串中的全部空格
StringTokenizer st = new StringTokenizer(s6, " "); // 使用空格分隔字符串
StringBuffer sb = new StringBuffer();
while (st.hasMoreTokens()) {
sb.append(st.nextToken());
}
System.out.println("\"" + s6 + "\"" + "length = " + s6.length()); // " Hello Java "length = 14
System.out.println("\"" + s7 + "\"" + "length = " + s7.length()); // "Hello Java"length = 10
System.out.println("\"" + s8 + "\"" + "length = " + s8.length()); // "HelloJava"length = 9
System.out.println("\"" + sb.toString() + "\"" + "length = " + sb.toString().length()); // "HelloJava"length = 9
String sr = new String("abc abd bcd");
String sr2 = sr.replace("ab", "xx"); // 替换全部子串
String sr3 = sr.replaceFirst("ab", "xx"); // 替换第一个字串
System.out.println(sr2); // "xxc xxd bcd"
System.out.println(sr3); // "xxc adb bcd"
String se = new String("Summer is so Hot");
String se1 = new String("Summer is so Hot");
String se2 = new String("summer is so hot");
String se3 = se;
System.out.println(se == se1); // false 比较内存而非字符串内容
System.out.println(se == se3); // true
System.out.println(se.equals(se1)); // true 比较字符串内容
System.out.println(se.equals(se2)); // false
System.out.println(se.equalsIgnoreCase(se2)); // true 忽略大小写
System.out.println(se2.startsWith("summer")); // true 字符串开始
System.out.println(se2.endsWith("cold")); // false 字符串结尾
String sc = new String("hello WORLD");
String scl = sc.toLowerCase(); // hello world 转换为小写
String scu = sc.toUpperCase(); // HELLO WORLD 转换为大写
System.out.println(scl + " " + scu);
String ss = new String("abc,def,g,h");
String[] ss2 = ss.split(","); // 以逗号分隔
for (String x: ss2) {
System.out.print(x + "\t"); // abc def g h
}
Date d = new Date();
System.out.println(d); // Wed Jul 22 16:00:36 CST 2015 默认格式
System.out.println(String.format("%tm", d)); // 07 两位数月份
System.out.println(String.format("%tH", d)); // 16 两位24小时制
System.out.println(String.format("%x", 256)); // 100 十六进制
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有