博主老早就想总结一下,但是迫于没时间,感觉只是给自己找借口,但是现在花了一上午,终于整理好了,而且注释比较详细,废话不多说,现在上代码:
首先,博主自定义了一个bean类,为了测试自定义的那个list
这个bean里面博主把常用的类型基本都用了,但是不建议大家定义的时候使用float,真心很麻烦
另外就是get/set这一块,注意boolean类型是is和set,不是get,这个大家注意下
package cn.edu.sjzc.weiaccept.bean; /** * Created by fanyafeng on 2015/5/8/0008. */ public class FanyafengBean implements Comparable{ private int Age; private long Height; private float Phone; private double hair; private String Name; private boolean Sex; public FanyafengBean(int age, long height, float phone, double hair, String name, boolean sex) { Age = age; Height = height; Phone = phone; this.hair = hair; Name = name; Sex = sex; } public int getAge() { return Age; } public void setAge(int age) { Age = age; } public long getHeight() { return Height; } public void setHeight(long height) { Height = height; } public float getPhone() { return Phone; } public void setPhone(float phone) { Phone = phone; } public double getHair() { return hair; } public void setHair(double hair) { this.hair = hair; } public String getName() { return Name; } public void setName(String name) { Name = name; } public boolean isSex() { return Sex; } public void setSex(boolean sex) { Sex = sex; } @Override public String toString() { return "FanyafengBean{" + "Age=" + Age + ", Height=" + Height + ", Phone=" + Phone + ", hair=" + hair + ", Name='" + Name + '\'' + ", Sex=" + Sex + '}'; } @Override public int compareTo(Object another) { return 0; } }
这是博主定义的bean类,因为涉及到顺序,所以要实现一个java.lang中的comparable,详情请看
http://blog.csdn.net/qq_23195583/article/details/44174181
再有就是具体的方法的书写了:
来看第一个:
简单的int,string
private static void intOrString() { /** * 简单罗列了一下一维数组的遍历 * int,String, * 一个是普通的for循环,一个是增强的for循环 */ int int_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; for (int arrayItem : int_array) { System.out.print(arrayItem + ","); } System.out.println(); System.out.println("-------------------------------------------------------------------"); for (int i = 0; i < int_array.length; i++) { System.out.print(int_array[i] + ","); } System.out.println(); System.out.println("-------------------------------------------------------------------"); String string_array[] = {"樊亚风", "软件工程", "java", "Android", "IOS"}; for (String arrayItem : string_array) { System.out.print(arrayItem + ","); } System.out.println(); System.out.println("-------------------------------------------------------------------"); for (int i = 0; i < string_array.length; i++) { System.out.print(string_array[i] + ","); } System.out.println(); System.out.println("-------------------------------------------------------------------"); }
第二个:
这里面包含的比较多
注释里面都有
private static void growing() { /** * 自定义一个类,里面包含了常用的类型 */ int Age = 0; long Height = 180; float Phone = 15530128888f; double hair = 3000d; String Name = "樊亚风"; boolean Sex = true; String sex; List<Map<String, Object>> fanyafengList = new ArrayList<Map<String, Object>>(); List<FanyafengBean> fanyafengBeans = new ArrayList<FanyafengBean>(); FanyafengBean[] fanyafengBeanArray = new FanyafengBean[]{ new FanyafengBean(Age, Height, 18032278888f, 3000d, Name, Sex), new FanyafengBean(22, 180, 18032278888f, hair, "樊亚风", true), new FanyafengBean(23, Height, 15530124556f, 3000d, Name, Sex), new FanyafengBean(24, 180, Phone, 2000d, "樊亚风", true) }; /** * 增强的for循环 */ for (FanyafengBean fanyafengBeanItem : fanyafengBeanArray) { System.out.print(fanyafengBeanItem.getAge() + ","); System.out.print(fanyafengBeanItem.getHeight() + ","); System.out.print(fanyafengBeanItem.getPhone() + ","); System.out.print(fanyafengBeanItem.getHair() + ","); System.out.print(fanyafengBeanItem.getName() + ","); if (fanyafengBeanItem.isSex()) { sex = "男"; } else { sex = "女"; } System.out.print(sex); System.out.println(); } System.out.println("-------------------------------------------------------------------"); /** * 普通的for循环遍历 * 以及一些list,map * 还有自定义类的数组 */ for (int i = 0; i < 4; i++) { FanyafengBean fanyafengBean = new FanyafengBean(Age, Height, Phone, hair, Name, Sex); Map<String, Object> map = new HashMap<String, Object>(); map.put("myage", fanyafengBeanArray[i].getAge()); System.out.print(fanyafengBeanArray[i].getAge() + ","); map.put("myheight", fanyafengBeanArray[i].getHeight()); System.out.print(fanyafengBeanArray[i].getHeight() + ","); map.put("myphone", fanyafengBeanArray[i].getPhone()); System.out.print(fanyafengBeanArray[i].getPhone() + ","); map.put("myhair", fanyafengBeanArray[i].getHair()); System.out.print(fanyafengBeanArray[i].getHair() + ","); map.put("myname", fanyafengBeanArray[i].getName()); System.out.print(fanyafengBeanArray[i].getName() + ","); map.put("mysex", fanyafengBeanArray[i].isSex()); if (fanyafengBeanArray[i].isSex()) { sex = "男"; } else { sex = "女"; } System.out.print(sex); System.out.println(); fanyafengList.add(map); } Arrays.sort(fanyafengBeanArray); fanyafengBeans = Arrays.asList(fanyafengBeanArray); System.out.println(fanyafengBeans.toString()); System.out.println("-------------------------------------------------------------------"); }
第三个
二维数组
private static void doubleArray() { /** * 二位数组 */ int darray[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {0}}; for (int i = 0; i < darray.length; i++) { for (int j = 0; j < darray[i].length; j++) { System.out.print(darray[i][j]); } System.out.println(); } System.out.println("-------------------------------------------------------------------"); for (int da[] : darray) { for (int daItem : da) { System.out.print(daItem); } System.out.println(); } System.out.println("-------------------------------------------------------------------"); }
第四个:
这里面是list数组,涉及到了迭代器使用,遍历
private static void list() { /** * list数组 * 迭代器,增强for循环 */ List<String> list = new ArrayList<>(); list.add("樊亚风"); list.add("正在学习"); list.add("IOS"); Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { String strList = (String) iterator.next(); System.out.println(strList); } System.out.println("\n" + "-------------------------------------------------------------------"); for (String item : list) { System.out.print(item); } System.out.println(); System.out.println("-------------------------------------------------------------------"); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)); } System.out.println(); System.out.println("-------------------------------------------------------------------"); }
第五个
说一下网络层编写常用的
详情看注释
private static void linkmap() { /** * 这里强调必须要初始化,否则程序过不去 * 但是在些项目的时候我很少初始化 * 的确都是先定义 */ String name = "fanyafeng"; String major = "software"; String url = "detail"; StringBuffer myurl = new StringBuffer(url); StringBuffer myurl1 = new StringBuffer(url); myurl.append(":"); Map<String, String> map = new LinkedHashMap<String, String>(); map.put("name", name); map.put("major", major); for (Map.Entry<String, String> entry : map.entrySet()) { myurl.append(entry.getKey()).append("="); myurl.append(entry.getValue()); //网络获取切记用linkedhashmap,否则容易出问题 // myurl.append(URLEncoder.encode(entry.getValue(), "UTF-8")); myurl.append("&"); } myurl.deleteCharAt(myurl.length() - 1); System.out.print(myurl.toString()); System.out.println("\n" + "--------------------------------------------"); Map<String, String> map1 = new HashMap<String, String>(); map1.put("name", name); map1.put("major", major); for (Map.Entry<String, String> entry : map1.entrySet()) { myurl1.append(entry.getKey()).append("="); myurl1.append(entry.getValue()); myurl1.append("&"); } myurl.deleteCharAt(myurl.length() - 1); System.out.print(myurl.toString()); System.out.println("\n" + "--------------------------------------------"); }
第六个
迭代器和map的使用
private static void it() { /** * 迭代器 */ Map map = new HashMap(); for (int i = 0; i < 10; i++) { map.put(i, new String("map" + i)); } Iterator iterator=map.entrySet().iterator(); while (iterator.hasNext()){ Map.Entry strMap=(Map.Entry)iterator.next(); System.out.print(strMap.getValue()+","); } System.out.println("\n"+"----------------------------------"); }
第七个就是主方法,以及需要引入的类
import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * Created by fanyafeng on 2015/5/8/0008. */ public class TestClass { public static void main(String[] args) { System.out.println("测试主程序"); System.out.println("-------------------------------------------------------------------"); intOrString(); growing(); doubleArray(); list(); linkmap(); it(); }
最后是程序截图
时间: 2024-10-23 06:30:27