当前位置: 移动技术网 > IT编程>移动开发>Android > android中的键值对实例讲解

android中的键值对实例讲解

2018年09月19日  | 移动技术网IT编程  | 我要评论

十一年铃声,cz6959航班,东坡湖畔

hashmap,contentvalue,namevaluepair,jsonobject

arraylist和hashmap的区别:

内部元素:arraylist储存的是单个对象(此对象是可以通过设置对象类进而封装各种数据的),即arraylist al = new arraylist();

而hashmap储存的是一组一组的key和value,像:hashmap hm = new hashmap();

查找效率:hashmap的效率高些,因为它是散列存储的复杂度比较低,而arraylist是顺序存储的。arraylist是有序的,而hashmap无序。

继承接口:hashmap是继承的map接口,存放的是且不允许key为null。arraylist是继承的list接口,存储形式类似链表,允许随机的数据访问。(hashmap是允许使用 null 值和 null 键的!hashtable是不允许的!)

例如:

import java.util.hashmap;
public class student {
string name;
string sex;
public student(string n,string s) {
name=n;
sex=s;
}
public string tostring(){
return ("姓名:"+name+"\n"+"性别:"+sex+"\n"); 
}
public static void main(string [] args){
arraylist al=new arraylist();
hashmap hm=new hashmap();
student s1=new student("张三","男");
student s2=new student("李四","男");
student s3=new student("小利","女");
//存值是根据学生编号加上学生信息这样的一组信息
hm.put("001",s1);
hm.put("002",s2);
hm.put("003",s3);
//存值是直接存入一个对象实例
al.add(s1);
al.add(s2);
al.add(s3);
//查找学生编号是001的学生

//因为hm.get("001")反回的是object所以加上强转
student s=(student)hm.get("001");//通过键名来取
student s1=(student)al.get(0);//类似数组通过下标来取
system.out.println(s.tostring());
}
}

android中的namevaluepair(使用url进行数据传输)和contentvalues(写入的时候的内容组装)以及jsonobject(用于流数据传输的时候)、

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网