数组 --------集合 中的各种遍历方法!!!!!
来源:程序员人生 发布时间:2014-12-10 08:33:11 阅读次数:3135次
(包名用了中文,大家不要在乎这个)
1.数组
1.for
2.增强for
package 杨彬_数组;
public class ArrayDemo {
/*
* 数组遍历的方式: 1.for 2.增强for
*/
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
test1();
test2(arr);
}
//增强for
private static void test2(int[] arr) {
for(int i : arr){
System.out.println(i);
}
}
//for
private static void test1() {
int[] arr = { 1, 2, 3, 4, 5 };
for (int i = 0; i< arr.length; i++) {
System.out.println(arr[i]);
}
}
}
2.Collection
1.for
2.增强for
3.迭代器
4.toArray
package 杨彬_Collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/*
* 遍历方式 : 1. for(不写了) 2,增强for 3.迭代器 4.toArray
*/
public class CollectionDemo {
public static void main(String[] args) {
Collection<String> c = new ArrayList<String>() ;
c.add("杨彬");
c.add("郭乐乐");
c.add("科比");
c.add("乔丹");
// test1(c);
// test2(c);
test3(c);
}
//toArray
private static void test3(Collection<String> c) {
Object[] cc = c.toArray();
for(int i=0;i<cc.length;i++){
System.out.println(cc[i]);
}
}
//迭代器
private static void test2(Collection<String> c) {
Iterator<String> it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
//增强FOR
private static void test1(Collection<String> c) {
for(String c1 : c){
System.out.println(c1);
}
}
}
3.List
package 数组_List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/*
* for
* 增强for
* 迭代器
* list特有迭代器
* 角标get()
* toArray()
*/
public class ListDemo {
public static void main(String[] args) {
List<String> c = new ArrayList<String>() ;
c.add("杨彬");
c.add("郭乐乐");
c.add("科比");
c.add("乔丹");
// test1(c);
// test2(c);
// test3(c);
// test4(c);
test5(c);
test6(c);
}
private static void test6(List<String> c) {
for(ListIterator<String> lit = c.listIterator(); lit.hasNext();){
System.out.println(lit.next());
}
}
//toArray()
private static void test5(List<String> c) {
Object[] array = c.toArray();
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
//角标get();
private static void test4(List<String> c) {
for(int i=0;i<c.size();i++){
System.out.println(c.get(i));
}
}
//LIst迭代器
private static void test3(List<String> c) {
ListIterator<String> lsc = c.listIterator();
while(lsc.hasNext()){
System.out.println(lsc.next());
}
}
//迭代器
private static void test2(List<String> c) {
Iterator<String> it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
//增强for
private static void test1(List<String> c) {
for(String c1: c){
System.out.println(c1);
}
}
}
3.Set
package 杨彬_Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/*
* 迭代器
* 增强for
* toArray
*
*/
public class SetList {
public static void main(String[] args) {
Set<String> c = new HashSet<String>() ;
c.add("杨彬");
c.add("郭乐乐");
c.add("科比");
c.add("乔丹");
// test2(c);
//test1(c);
test3(c);
}
//toArray()
private static void test3(Set<String> c) {
Object[] array = c.toArray();
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
//增强for
private static void test2(Set<String> c) {
for(String s:c){
System.out.println(s);
}
}
//迭代器
private static void test1(Set<String> c) {
Iterator<String> it = c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
4.Map
package 杨彬_Map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
/* VAlues
* keyset
* entryset
*/
public class MapDemo {
public static void main(String[] args) {
Map<Integer, String> c = new HashMap<Integer, String>() ;
c.put(1,"杨彬");
c.put(2,"郭乐乐");
c.put(3,"科比");
c.put(4,"乔丹");
// test1(c);
// test2(c);
test3(c);
}
private static void test3(Map<Integer, String> c) {
for(Entry<Integer, String> entry : c.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
//keyset
private static void test2(Map<Integer, String> c) {
Set<Integer> set = c.keySet();
for(Integer key : set){
String value1 = c.get(key);
System.out.println(key+":"+ value1);
}
}
//Values 只能取值
private static void test1(Map<Integer, String> c) {
Collection<String> co = c.values();
for(String s : co){
System.out.println(s);
}
}
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠