JDK6.0的巩固学习演示类Demo1(Java JDK6巩固学习笔记第1-5章节内容!)
Posted on 2008-12-31 13:14 qiqi 阅读(263) 评论(0) 编辑 收藏 举报
/**
* 该类为:JDK6.0的巩固学习演示类Demo1
* Java JDK6巩固学习笔记第1-5章节内容!
* **/
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;
public class JDK6Demo1 {
public static void main(String [] args)
{
Scanner scanner = new Scanner(System.in);
Date dt = new Date();
System.out.println("现在的时间是:" + dt.toString());
String text ="Have a nice day !!!";
System.out.println("原文:" + text);
// 内容大写
System.out.println("大写:" + text.toUpperCase());
// 内容小写
System.out.println("小写:" + text.toLowerCase());
// 获取长度
System.out.println("长度:" + text.length());
// 取代文字
System.out.println("取代:" + text.replace("nice", "Good"));
/* System.out.println(" 使用者名称:");
String username = scanner.next();
System.out.println(" 用户密码:");
String password = scanner.next();
if("haha".equals(username) && "123".equals(password)){
System.out.println("数据均正确,登陆OK!");
}else{
System.out.println("输入的用户名或密码错误,请重新输入!谢谢!");
}
*/
// 自动装箱、拆箱
Integer integer = 10; //JDK5.0后的自动装箱
Integer integer1 = new Integer(10); //JDK5.0前的方式
System.out.println(integer.TYPE);
System.out.println(integer1.TYPE);
if (integer.equals(integer1)){
System.out.println("两种方式得到的结果一样!");
}else{
System.out.println("两种方式得到的结果是不一样!");
}
// 数组的相关演示
int[] arr1 = {1,2,3,4,5};
int[] arr2 = new int[5];
for (int i = 0 ; i < arr1.length; i++)
arr2[i] = arr1 [i];
System.out.println("JDK5.0以后循环显示数组的方法: " );
System.out.println("=============================================" );
// JDK5.0以后的循环显示一维数组的方法
for (int element:arr1)
{
System.out.print(element + " ");
System.out.println();
}
System.out.println("=============================================" );
for (int i = 0 ; i < arr2.length; i++)
System.out.print(arr2[i] + " ");
System.out.println();
// 二维数组的相关演示
int[][] array1 = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int[][] array2 = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int[][] array3 = {
{0,1,3},
{4,5,6},
{7,8,9}
};
// 数组的比较不可以用"=="号,要用deepEquals进行比较
System.out.println("array1和array2相等:" + Arrays.deepEquals(array1, array2));
System.out.println("array1和array3相等:" + Arrays.deepEquals(array1, array3));
System.out.println(Arrays.deepToString(array1));
// foreach与数组
/*
* for(type element:array){
* System.out.println(element);
* }
*
* * J2SE 5.0之前
int[] arr = {1, 2, 3, 4, 5};
for(int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
* 在J2SE5.0之后
int[] arr = {1, 2, 3, 4, 5};
for(int element : arr)
System.out.println(element);
*
* * 如果是对象的话
String[] names = {"caterpillar", "momor", "bush"};
for(String name : names)
System.out.println(name);
* 二维数组
int[][] arr = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
for(int[] row : arr) {
for(in element : row) {
System.out.println(element);
}
}
*/
// 二维数组循环打印显示
System.out.println("二维数组循环打印演示:");
System.out.println("===================================================");
for(int [] row : array3){
for (int element : row){
System.out.println(element + " ");
}
}
System.out.println("===================================================");
// 数组中的元素是对象的(一维)演示
System.out.println("数组中的元素是对象的(一维)演示:");
System.out.println("===================================================");
String[] names = {"caterpillar", "momor", "bush"};
for(String name : names)
System.out.println(name);
System.out.println("===================================================");
// 数组中的元素是对象的(二维)演示
System.out.println("数组中的元素是对象的(二维)演示:");
System.out.println("===================================================");
String[][] nameString = {{"caterpillar", "momor", "bush"},
{"yang", "li", "zhang"}
};
for(String []row : nameString){
for (String element : row){
System.out.println(element + " ");
}
}
}
}