CollectionExer(判断集合元素的属性字符串)

2019-09-26  本文已影响0人  勇者与王者

package collectionDemo;

import java.util.ArrayList;

/**
* 
* @author q06285:
* @version 创建时间:2019年9月26日 下午4:52:33
* 
*/
public class CollectionExer {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        ArrayList<Hero> heros = new ArrayList<>();
        
        for (int i = 0 ; i< 5;i++) {
            heros.add(new Hero("hero "+i));
        }
        
//      System.out.println(heros);
//      
//      for (Hero h1 : heros) {
//          System.out.println(h1.name);
//      }
        
        for (Hero h: heros) {
            //字符串比较使用equals 不能使用  ==
            if (h.name.equals("hero 1")) {
                System.out.println("找到了name是 hero 1 的对象 "+
                        heros.indexOf(h));
            }
        }
        
        
        
        
    }

}

上一篇 下一篇

猜你喜欢

热点阅读