package JavaTest.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.junit.Test;
public class JavaBase {
//@Test
public void stringTest(){
int i=0;
String str=i+"";
System.out.println(str.getClass());
Integer num = Integer.valueOf(str);
num++;
System.out.println(num.getClass());
String totalSize =null;
System.out.println(stringToNumber(totalSize));
}
public int stringToNumber(String str){
//参数字符串,先判断为空或者为null的情况,否则会报错
if(str!=null&&!str.equals("")){
return Integer.valueOf(str);
}
return 0;
}
//@Test
public void arrayTest(){
int[][] data1 = new int[][]{{1}};
Object[][] data2=new Integer[][]{{1,2},{3,4}};
Integer[][] data3 = new Integer[][]{{1,2,3},{4,5}};
Integer[][] data4 = new Integer[5][5];
for(int i=0,max=data3.length;i<max;i++){
for(int j=0,max2=data3[i].length;j<max2;j++){
//System.out.println(data3[i][j]);
}
}
int t=0;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++){
data4[i][j]=t++;
}
}
for(Integer[] row:data4){
for(Integer data:row){
System.out.print(data+" ");
}
System.out.println();
}
}
//@Test
public void arraylistTest(){
List datalist = new ArrayList();
datalist.add(1);
datalist.add("2");
datalist.add(true);
datalist.add(new User());
for(int i=0;i<datalist.size();i++){
System.out.println(datalist.get(i));
}
List<Integer> dataList2 = new ArrayList<Integer>();
dataList2.add(1);
for(Integer i:dataList2){
System.out.println(i);
}
}
class User{
private Integer UserID;
private String UserName;
public User() {
}
public User(int i, String string) {
this.UserID=i;
this.UserName=string;
}
public Integer getUserID() {
return UserID;
}
public void setUserID(Integer userID) {
UserID = userID;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
}
//@Test
public void stringTest1(){
String str1="testfan";
String str2="tESTfAN";
String str3=null;
System.out.println(str2.equals(str1));
System.out.println(str1.equalsIgnoreCase(str2));
//不确定字符串是否为null,需要先于null进行比较
if(str2!=null&&str2.trim().equals(str1)){
System.out.println("str2 equal str1");
}
if("".equals(str3)){
System.out.println("str 3 is blank");
}
//下面这种情况会报空指针异常,一般把比较对象放在后面,如上面
if(str3.equals("")){
System.out.println("str 3 is blank");
}
}
//@Test
public void equalsTest(){
int num1=1;
Integer num2 =2;
if(num2.equals(num1)){
System.out.println("equals");
}
//num1.equals(num2) 这样写是错的,只有对象才有方法,一个数字是没有方法的
System.out.println(num2.equals(1));//如果数据类型不同,是没有意义的,永远为false
}
@Test
public void mapTest(){
Map usermap = new HashMap();
usermap.put(1,new User(1,"testfan"));
usermap.put(2, new User(2,"AryaZ"));
usermap.put(3, "hello");
// User user = (User)usermap.get(1);
// System.out.println(user.getUserName());
//
for(Object key:usermap.keySet()){
Object value = usermap.get(key);
if (value instanceof User) {
User u= (User)value;
System.out.println(u.getUserName());
}else {
System.out.println(value);
}
}
//-----------------------------hash map key无序,如果key是字符串,可以用treemap 排序
Map<Integer, User> usermap2 = new HashMap<Integer, JavaBase.User>();
usermap2.put(1,new User(1,"testfan2"));
usermap2.put(2, new User(2,"AryaZ2"));
System.out.println(usermap2.get(1).getUserName());
for(Map.Entry<Integer, User> entry : usermap2.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue().getUserName());
}
//----------------------------- tree map ,会按照key的字典书序进行升序
TreeMap sortedPara = new TreeMap();
sortedPara.put("device_token", "123456");
sortedPara.put("mobile", "187012345678");
sortedPara.put("version", "app_ios_1.1");
sortedPara.put("pass", "22222");
//遍历排序后的字典,将所有参数按“key=value”格式拼接
//stringBuild 来拼接字符串
StringBuilder accum = new StringBuilder();
for(Object key:sortedPara.keySet()){
accum.append(key).append("=").append(sortedPara.get(key));
}
System.out.println(accum);
}
}