package org.zl.br;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
*
* @author zl
* 数组里有{1,2,3,4,5,6,7,8,9,10},请随机打乱顺序,生成一个新的数组。
*
*/
public class ZLTest01 {
public static void main(String[] args) {
String[] s = new String[]{"1","2","3","4","5","6","7","8","9","10"};
/**
* 将数组转化为List
* 注意
* 1、该方法不适用于基本数据类型(byte,short,int,long,float,double,boolean)
* 2、不支持add和remove方法
* 3、该方法将数组与列表链接起来,当更新其中之一时,另一个自动更新
*/
List list = Arrays.asList(s);
Collections.shuffle(list);
System.out.println(list.toString());
}
}
package org.zl.br;
/**
*
* 写出代码判断一个整数是不是2的阶次方,(谢绝调用API方法)
* @author zl
*
*/
public class ZLTest02 {
public static void main(String[] args) {
int num=20;
if(((num-1) & num) == 0){
System.out.println("是2的阶次方");
}else{
System.out.println("不是2的阶次方");
}
}
}
package org.zl.br;
/**
*
* 有两个篮子,分别为A和B,篮子A里装着鸡蛋,篮子B里装着苹果,请用面向对象 的思路实现两个篮子里的物品交换.
* @author zl
*
*/
public class ZLTest05 {
private String s;
public ZLTest05() {
super();
}
public ZLTest05(String s) {
super();
this.s = s;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
@Override
public String toString() {
return "ZLTest05 [s=" + s + "]";
}
public static void main(String[] args) {
ZLTest05 A = new ZLTest05("鸡蛋");
ZLTest05 B = new ZLTest05("苹果");
String gA = A.getS();
String gB = B.getS();
System.out.println(gA+" : "+gB);
A.setS(gB);
B.setS(gA);
String gA2 = A.getS();
String gB2 = B.getS();
System.out.println(gA2+" : "+gB2);
}
}
package org.zl.br;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*
* 假设今日是2015年3月1日,星期日,请算出13个月零6天后是星期几,距离现在多少秒.(谢绝调用API方法)
*
* @author zl
*
*/
public class ZLTest04 {
public static void main(String[] args) {
YesAPI();
System.out.println("=====================================");
NoAPI();
}
public static void YesAPI() {
try {
Date date1 = (new SimpleDateFormat("yyyy年MM月dd日").parse("2015年3月1日"));
System.out.println("设置时间:" + date1);
Date date2 = (new SimpleDateFormat("yyyy年MM月dd日").parse("2016年4月7日"));
System.out.println(date2);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date2);
System.out.println("是周" + (calendar.get(Calendar.DAY_OF_WEEK) - 1));
long time = (date2.getTime() - date1.getTime()) / 1000;
System.out.println("秒数:" + time);
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void NoAPI() {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar);
Date date = new Date();
calendar.set(2015, (3 - 1), 1);
date = calendar.getTime();
System.out.println("设置时间:" + date);
calendar.add(Calendar.MARCH, 13);
calendar.add(Calendar.DAY_OF_YEAR, 6);
System.out.println("今天是周" + (calendar.get(Calendar.DAY_OF_WEEK) - 1));
long time1 = calendar.getTimeInMillis();
long time2 = date.getTime();
long time = (time1 - time2) / 1000;
System.out.println(time);
}
}
package org.zl.br;
/**
*
* 实现一个排序方法,能对任意单一类型数组排序,比如Integer[],String[],Long[]或者其他任意类型的数组代码不做限制
* 可随意发挥(谢绝调用函数的方法,用循环语句实现).
* @author zl
*
*/
public class ZLTest03 {
public static void main(String[] args) {
int[] o = new int[]{1,11,3,22,5,31,7,8,9,10};
for(int i=0; i<o.length-1; i++){
for(int j=0; j<o.length-1-i; j++){
if(o[j] > o[j+1]){
int temp = o[j];
o[j] = o[j+1];
o[j+1] = temp;
}
}
}
for(int i=0; i<o.length; i++){
System.out.print(o[i]+" ");
}
}
}