java遍历数组怎么取指定元素,object[] ,li.[i] 然后怎么取某个元素的值
Object[] myobj = {1,2,3,4};
String str;
for(int i = 0; i < myobj.length; i++){
Object obj = myobj[i];
str = obj.toString();
System.out.println(str);
}
其中:
Object[] myobj 得到对象数组
Object obj = myobj[i];得到对象数组中每个对象
public class For {
public int i;
public For(int i){
this.i=i;
}
public static void main(String[] args) {
For[] arr = new For[]{new For(1),new For(2),new For(3),new For(4)};
for(int index=0;index<arr.length;index++){
if(arr[index].i==3){};
}
}
}
public class Demo {
public static void main(String[] args) {
Object[] obj={1,"2",3.1415926,4,5,6,7,8,9};
int i=5;
String r="2";
double d=3.1415926;
for(Object o:obj){
if(o.equals(i)){
System.out.println(o);
}
if(o.equals(r)){
System.out.println(o);
}
if(o.equals(d)){
System.out.println(o);
}
}
}
}
最常见的是 for 遍历
List list ;//你给他初始化几个对象就行了
for(int i=i0; i<list.size() ;i++)
{
Student s = list.get(i);
//对象就拿出来了
}