赞
踩
在Java中可以定长的来初始化一个数组,并可以使用下标index来赋值或取值,使用非常方便。示例代码如下:
- Object[] objects = new Object[2];
- objects[0] = 100;
- objects[1] = "chen";
-
- System.out.println("objects:" + objects);
- System.out.println("object[0]:" + objects[0]);
- System.out.println("object[1]:" + objects[1]);
-
- int[] ints = new int[2];
- ints[0] = 10;
- ints[1] = 11;
- System.out.println("ints[0]:" + ints[0]);
- System.out.println("ints[1]:" + ints[1]);
-
- String[] strings = new String[2];
- strings[0] = "chen";
- strings[1] = "yufeng";
- System.out.println("strings[0]:" + strings[0]);
- System.out.println("strings[1]:" + strings[1]);
- //List元素是有序的,元素可以重复;
- //ArrayList底层数据结构是数组;
- List<String>list = new ArrayList<String>();
- list.add("chen");
- list.add("yufeng");
- System.out.println("list:" + list);
- System.out.println("list(0):" + list.get(0));
- System.out.println("list(1):" + list.get(1));
在一门成熟的语言中,一般都支持键值对存储,在iOS中是Dictionary,而在Java中则是Map。Map的示例代码如下:
- Map<String, String> map = new HashMap<String, String>();
- map.put("1", "chen");
- map.put("2", "yufeng");
- System.out.println("map:" + map);
-
- System.out.println("map(1):" + map.get("1"));
- System.out.println("map(2):" + map.get("2"));
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。