冒泡排序:
package zz.itheima.arrays;
public class BubbleSort {
public static void main(String[] args) {
int score[] = {67, 69, 75, 87, 89, 90, 99, 100};
for (int i = 0; i < score.length -1; i++){
for(int j = 0 ;j < score.length - i - 1; j++){
if(score[j] < score[j + 1]){
int temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
System.out.print("第" + (i + 1) + "次排序结果:");
for(int a = 0; a < score.length; a++){
System.out.print(score[a] + "\t");
}
System.out.println("");
}
System.out.print("最终排序结果:");
for(int a = 0; a < score.length; a++){
System.out.print(score[a] + "\t");
}
}
}
运行结果:
第1次排序结果:69 75 87 89 90 99 100 67
第2次排序结果:75 87 89 90 99 100 69 67
第3次排序结果:87 89 90 99 100 75 69 67
第4次排序结果:89 90 99 100 87 75 69 67
第5次排序结果:90 99 100 89 87 75 69 67
第6次排序结果:99 100 90 89 87 75 69 67
第7次排序结果:100 99 90 89 87 75 69 67
最终排序结果:100 99 90 89 87 75 69 67
选择排序:
package zz.itheima.arrays;
public class SelectSort {
/** * 选择排序 * @param args */
public static void main(String[] args) {
int[] arr={3,5,1,7,6};
for(int j=0;j<arr.length-1;j++)
{
for (int i = j+1; i < arr.length; i++) {
if(arr[j]>arr[i])
{
int temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
运行结果:
1
3
5
6
7
顺序查找:
package zz.itheima.arrays;
public class OrderSearch {
/** * 顺序查找 * @param args */
public static void main(String[] args) {
int[] arr={3,5,1,7,6};
boolean flag=false;
for (int i = 0; i < arr.length; i++) {
if(arr[i]==7)
{
System.out.println("存在");
flag=true;
break;
}
}
if(flag==false)
System.out.println("不存在");
}
}
运行结果:
存在
二分折半查找:
package zz.itheima.arrays;
public class BinarySearch {
/** * 二分折半查找 * @param args */
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9};
int left=0;
int right=arr.length-1;
while (left<=right) {
int mid=(left+right)/2;
if(arr[mid]==8)
{
System.out.println("存在");
break;
}
if(arr[mid]<10)
{
left=mid+1;
}
if(arr[mid]>10)
{
right=mid-1;
}
}
if(left>right)
System.out.println("不存在");
}
}
运行结果:
存在
测试程序运行时间
package zz.itheima.arrays;
public class TestSystem {
public static void main(String[] args) {
int[] arr={3,5,1,7,6,9,10,67,34,55,99};
long begin=System.currentTimeMillis();
for(int j=0;j<arr.length-1;j++)
{
for (int i = 0; i < arr.length-1-j; i++) {
if(arr[i]>arr[i+1])
{
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
long end=System.currentTimeMillis();
System.out.println(end-begin);
}
}
数字格式化:
package zz.itheima.arrays;
import java.text.NumberFormat;
public class TestNumberFormat {
public static void main(String[] args) {
double a=10.98;
System.out.println(a*1.1);
NumberFormat nf=NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
System.out.println(nf.format(a*1.1));
}
}
运行结果:
12.078000000000001
12.1
日期格式化:
package zz.itheima.arrays;
import java.text.DateFormat;
import java.util.Date;
import java.util.Scanner;
public class TestDateFormat {
public static void main(String[] args) throws Exception {
Date now=new Date();
System.out.println(now);
DateFormat df1=DateFormat.getDateInstance();
String s=df1.format(now);
System.out.println(s);
DateFormat df2=DateFormat.getDateInstance(DateFormat.SHORT);
s=df2.format(now);
System.out.println(s);
DateFormat df3=DateFormat.getDateTimeInstance();
s=df3.format(now);
System.out.println(s);
DateFormat df4=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.SHORT);
s=df4.format(now);
System.out.println(s);
Scanner sc=new Scanner(System.in);
System.out.println("请输入你的生日:");
String birString=sc.nextLine();
DateFormat df5=DateFormat.getDateInstance();
Date birDate=df5.parse(birString);
System.out.println(birDate);
}
}
运行结果:
Thu Nov 26 17:03:42 CST 2015
2015-11-26
15-11-26
2015-11-26 17:03:42
2015年11月26日 星期四 下午5:03
请输入你的生日:
时间测试:
package zz.itheima.arrays;
import java.text.DateFormat;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
Date now=new Date();
System.out.println(now);
System.out.println(now.getTime());
System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.SHORT).format(now));
}
}
运行时间:
Thu Nov 26 17:04:40 CST 2015
1448528680749
2015年11月26日 星期四 下午5:04
测试日历:
package zz.itheima.arrays;
import java.util.Calendar;
import java.util.Date;
public class TestCalender {
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
System.out.println(c);
System.out.println(c.get(Calendar.HOUR));
c.set(Calendar.YEAR,2015);
Date d=c.getTime();
System.out.println(d);
}
}
运行结果:
java.util.GregorianCalendar[time=1448528724370,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=10,WEEK_OF_YEAR=48,WEEK_OF_MONTH=4,DAY_OF_MONTH=26,DAY_OF_YEAR=330,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=5,SECOND=24,MILLISECOND=370,ZONE_OFFSET=28800000,DST_OFFSET=0]
5
Thu Nov 26 17:05:24 CST 2015
数组类进行测试:
package zz.itheima.arrays;
import java.util.Arrays;
public class TestArrays {
/** * 对数组类进行测试 * @param args */
public static void main(String[] args) {
int[] arr={4,6,1,8,3,9};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
int num=0;
int result=Arrays.binarySearch(arr, num);
System.out.println(result);
int[] temp=Arrays.copyOf(arr, 3);
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i]+" ");
}
System.out.println();
temp=Arrays.copyOfRange(arr, 2, 4);
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i]+" ");
}
System.out.println();
int[] arr2={4,6,1,8,3,9};
System.out.println(Arrays.equals(arr, arr2));
}
}
运行结果:
1 3 4 6 8 9
-1
1 3 4
4 6
false
数组存放对象:
package zz.itheima.arrays;
public class TestArray {
public static void main(String[] args) {
int[] arr={4,2,8,1,7};
Book[] arrBooks=new Book[3];
arrBooks[0]=new Book("adf",100.10);
arrBooks[1]=new Book("aasddf",10560.10);
arrBooks[2]=new Book("aasdfdf",10560.10);
int temp=arr[1];
arr[1]=arr[3];
arr[3]=temp;
}
}
两个日期时间差:
package zz.itheima.arrays;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Demo2 {
/** * 两个日期时间差 * @param args * @throws ParseException */
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1=sdf.parse("2015-07-13 10:10:10");
Date d2=sdf.parse("2015-11-26 00:00:00");
System.out.println(daysBetween(d1,d2));
System.out.println(daysBetween("2015-07-13 10:10:10","2015-11-26 00:00:00"));
}
/** * 计算两个日期之间相差的天数 * @param smdate 较小的时间 * @param bdate 较大的时间 * @return 相差天数 * @throws ParseException */
public static int daysBetween(Date smdate,Date bdate) throws ParseException
{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
smdate=sdf.parse(sdf.format(smdate));
bdate=sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/** *字符串的日期格式的计算 */
public static int daysBetween(String smdate,String bdate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
}
运行结果:
136
136
固定格式日历:
package zz.itheima.arrays;
import java.util.Calendar;
public class Demo1 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH)+1;
int day=c.get(Calendar.DAY_OF_MONTH);
int hour=c.get(Calendar.HOUR);
int minute=c.get(Calendar.MINUTE);
int sec=c.get(Calendar.SECOND);
System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+sec);
}
}
运行结果:
2015-11-26 5:9:7
Book类:
package zz.itheima.arrays;
public class Book {
private String name;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Book(String name, double price) {
super();
this.name = name;
this.price = price;
}
public Book() {
super();
}
}