一、集合
public static void main(String[] args) {
Set<String> s;
//初始化
s=new HashSet();
//添加数据
s.add("1");
s.add("2");
s.add("3");
s.add("3");
//System.out.println(s);
//定义迭代器
Iterator<String> it;
//获取集合s的迭代器
it=s.iterator();
//判断集合中是否
while(it.hasNext()){
System.out.println("value="+it.next().toString());
}
}
二、数组
public static void main(String[] args) {
//定义方式1
int[] arr1=new int[10];
//定义方式2
int[] arr2={1,2,3,4,5,6,7,8,9,9};
//遍历方法1
for(int i=0;i<arr1.length;i++){
arr1[i]=arr2[i];
}
System.out.println(arr1);
System.out.println(arr2);
for(int i=0;i<10;i++){
System.out.print(arr1[i]+",");
}
//遍历方法2
//把所有arr2里的值赋给j
System.out.println("\n");
for(int j:arr1){
System.out.print(j+",");
}
}
三、链表
//适合在任意位置插入
public static void main(String[] args) {
List<Integer> l=new ArrayList<Integer>();
//赋值方式1
for(int i=1;i<10;i++){
l.add(i);
}
//赋值方式2
l.add(111);
//下标4插入444
l.add(4, 444);
//输出方式1
System.out.println(l);
//输出方式2
for(int i=0;i<l.size();i++){
System.out.print(l.get(i)+",");
}
}
四、map
public static void main(String[] args) {
Map<String, Integer> map=new HashMap<String,Integer>();
for(int i=0;i<10;i++){
map.put(i+"", i);
}
map.put("10", 4);
//输出1
System.out.println(map);
//输出2
for(String key:map.keySet()){
//遍历key,通过key获取value
System.out.print(key+"="+map.get(key)+";");
}
System.out.println("\n");
//输出3
for(Entry<String,Integer> entry:map.entrySet()){
//定义一个map的入口,通过入口一个个遍历map里面的键和值
//entry类型和map类型相同
System.out.print(entry.getKey()+"="+entry.getValue()+";");
}
}
五、字符串的分割、数字链表转换
public static void main(String[] args) {
String str="0=01=12=23=34=45=56=67=78=89=910=4";
String[] s=str.split("=");
for(String s1:s){
System.out.println(s1+",");
}
//数组转链表
List<String> l=new ArrayList();
for(String s1:s){
l.add(s1);
}
System.out.println(l);
//链表转数组
String[] ss=new String[l.size()];
//方法1
for(int i=0;i<l.size();i++){
ss[i]=l.get(i);
}
//方法2
l.toArray(ss);
for(String j:ss){
System.out.print(j+"-");
}
}
六、json解析
public static void main(String[] args) {
String str="jQuery110209105712885626891_1523611573456({\"errNo\":\"0\",\"content\":\"0\"})";
str=str.substring(str.indexOf("{"), str.lastIndexOf("}")+1);
System.out.println(str);
JSONObject jsonObject=null;
Map<String, String> map=new HashMap<String,String>();
try{
jsonObject =new JSONObject(str);
}catch(Exception e){
e.printStackTrace();
}
if(jsonObject!=null){
Iterator<?> it=jsonObject.keys();
String key=null;
String value=null;
while(it.hasNext()){
key=it.next().toString();
try{
value=jsonObject.get(key).toString();
}catch(Exception e){
e.printStackTrace();
}
System.out.println(key+":"+value);
map.put(key, value);
}
}
System.out.println(map);
}