我有2个数组(a,b,c,x,y,z已经定义过了,这里省略,不需要加"")
String [] one ={a,b,c}; 
String [] two ={x,y,z}; 
我现在要让这个数组循环显示
for (int onei = 0; onei < one.length; onei++) {
   System.out.print(one[onei]);
}
问题是我x,y,z和a,b,c是同步信息,如何a是宝马,x就是z4,我想让他一个for循环显示出来,而不是for循环嵌套。
也就是
for(…………){
System.out.print(one[onei])System.out.print(two [two i])
}请问应该如何写??谢谢!!!实际打印出来应该是,比如:
宝马   z4
奔驰   600
奥迪   tt

解决方案 »

  1.   

    你的那两个数组大小应该是一样的。你直接用
    for(…………){ 
    System.out.print(one[onei])System.out.print(two [onei]) 

    就可以了。
      

  2.   

    public static void main(String[] args) {
    String [] one ={"a", "b", "c"}; 
    String [] two ={"x", "y", "z"}; 

    for(int i=0; i<one.length; i++)
    System.out.print(one[i] +"\t"+ two[i] +"\n");
        }
    这么做有一个潜在的问题,就是one和two的长度必须是一样的建议用HashMap类来做这样的事情
      

  3.   

    对,用hasMap,key 放one ={a,b,c};数组,value放two ={x,y,z}; 这样一一对应起来·
      

  4.   

    如果one数组和two数组长度一样的话
    for(…………){ 
    System.out.print(one[onei])System.out.println(two [two i]) 
    }
    就可以达到效果了,如果不一样的话,就要用到hashmap循环了。
      

  5.   

    import java.util.*;
    import java.util.Map.Entry;public class TestHashMap 
    {
    public static void main(String[] args) 
    {
    HashMap<String,String> hm = new HashMap<String,String>();
    hm.put("宝马","Z4");
    hm.put("奔驰","600");
    hm.put("奥迪","tt");

    Set<Entry<String,String>> set = hm.entrySet();
    Iterator<Entry<String,String>> it = set.iterator();
    while(it.hasNext())
    {
    Entry<String,String> entry = it.next();
    System.out.println(entry.getKey()+" "+entry.getValue()); 
    }
    }
    }
    结果:
    宝马  z4
    奔驰  600
    奥迪  tt
      

  6.   

    引用 2 楼 justinavril 的回复:
    Java codepublicstaticvoid main(String[] args) { 
            String [] one={"a","b","c"}; 
            String [] two={"x","y","z"};for(int i=0; i <one.length; i++) 
                System.out.print(one[i]+"\t"+ two[i]+"\n"); 
        } 
    这么做有一个潜在的问题,就是one和two的长度必须是一样的 建议用HashMap类来做这样的事情 
    同意此意见。数组会不会自我扩容?也就是自我扩充大小.......
      

  7.   


    java数组不可以,如果需要可以用arraylist默认10,加载因子1.25