java基础7

数组排序之冒泡排序
public class ArrayDemo {
	public static void main(String[] args){
		int[] arr = {24, 69, 89, 57, 13};
	
	sort(arr);
	System.out.println("排序后")printArray(arr);
	}
	
	public static void sort(int[] arr) {
		for(int x=0; x<arr.length-1; x++){
			for(int y=0; y<arr.legth-1-x; y++){
				if(arr[y]>arr[y+1]){
					int temp = arr[y];
					arr[y] = arr[y+1];
					arr[y+1] = temp;
				}
			}
		}
	}
	public static void printArray(int[] arr) {
		System.out.print("[");
		for(int x=0; x<arr.length; x++){
			if(x==arr.length-1){
				System.out.print(arr[x]);
			}else {
				System.out.print(arr[x]+", ");
			}
		}
	}
}
Array类
import java.util.Arrays;
/*
 *Arrays:提供了对数组操作的各种方法。
 *public static String toString(int[] a):把数组转出字符串
 *public static void sort(int[] a):对数组进行升序排序
 */
 public class ArrayDemo {
 	public static void main(String[] args){
 		int[] arr = {24, 69, 80, 57, 13};
 		System.out.println("排序前:"+Arrays.toString(arr));
 		Arrays.sort(arr);
 		System.out.println("排序后:"+Arrays.toString(arr));
 	}
 }

工具类的构造思想:
构造方法私有
成员都用static修饰。

基本类型包装类

为了对基本数据类型进行更多更方便的操作,Java就针对每一种基本数据类型提供了一个对应的引用类型。
基本类型包括:
Byte------byte
Short------short
Integer------int
Long------long
Float------float
Double------double
Character-------char
Boolean------boolean

public class IntegerDemo {
	public static void main(String[] args){
		System.out.println(Integer.MAX_VALUE);
		System.out.println(Integer.MIN_VALUE);
	}
}

基本类型包装类最常见的用法就是用于和字符串之间相互转换。

/*
*Integer:Integer类在对象中包装了一个基本类型int的值
*构造方法:
*	Integer(int value)
*	Integer(String s)
*/
public class IntegerDemo {
	public static void main(String[] args) {
		int value = 100;
		Integer i = new Integer(value);
		System.out.println(i);

		String s = "100";
		Integer ii = new Integer(s);
		System.out.println(ii);
	}
}
int和String之间相互转换
/*
*int类型和String类型的相互转换
*
*int -- String
*	String类中:public static String valueOf(int i)
*
*String -- int
*	Integer类中:public static int parseInt(String s)
*/
public class IntegerDemo {
	public static void main(String[] args){
		//int--String
		int number = 100;
		//方式1
		String s1 = "" + number;
		System.out.println(s1);
		//方式2
		//public static String valueOf(int i)
		String s2 = String.valueOf(number);
		System.out.println(s2);
	
		//String -- int
		String s = "100";
		//方式1
		//String -- 	Integer -- int
		Integer i = new Integer(s);
		//public int intValue()
		int x = i.intValue();
		System.out.println(x);
		//方法2
		//public static int parseInt(String s)
		int y = Integer.parseInt(s);
		System.out.println(y);
	}
}
自动装箱与自动拆箱

自动装箱:把基本数据类型转换为对应的包装类类型
public static Integer valueOf(int i)
自动拆箱:把包装类类型转换为对应的基本数据类型
public int intValue()

注意:在使用包装类类型的新特性时,如果做操作,最好判断是否为null
只要是对象,在使用前就必须进行不为null的判断。

public class IntegerDemo {
	public static void main(String[] args) {
		Integer ii = 100;  //自动装箱
		ii += 200;  //自动拆箱,自动装箱
		System.out.printlin(ii);
	}
}
Date类

Date: Date类表示特定的时间。
Date():根据当前时间创建日期对象。
Data(long data):根据给定的毫秒值创建对象。自1970年1月1日 00:00:00

/*
*SimpleDateFormat是一个以语言环境有关的方式来格式化和解析日期的具体类。
*它允许进行格式化(日期--文本),解析(文本--日期)和规范化。
*格式化(日期->文本):Date -- String
*	public final String format(Date date)
*解析(文本->日期):String -- Date
*	public Date parse(String sourse)
*/
public class SimpleDateFormatDemo {
	public static void main(String[] args)throws ParseException{
		/*
		//Date -- String
		Date d = new Date();
		//SimpleDateFormat(); 用默认的模式
		//SimpleDateFormat sdf = new SimpleDateFormat();
		//SimpleDateFormat(String pattern):用给定的模式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		String s = sdf.format(d);
		System.out.println(s);
		*/

		//String -- Date
		String str = "2008-09-09 09:21:45";
		//把一个字符串解析为日期的时候,请注意模式字符串和给定的日期字符串的格式要匹配
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = sdf.parse(str);
		System.out.println(d);
	}
}
		



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值