2014年5月17日星期六

java how cycling can can get to each property's value

using jdk1.4
For example:
public void people () {
User u = new User ();
u.setAge ("abc");
u.setName ("abc");
User u2 = new User ();
u2.setAge ("abc");
u2.setName ("abc");
List ls = new ArrayList ();
ls.add (u);
ls.add (u2);
/ / iterate
for (int i = 0; i User us = ls.get (i);
String age = us.getAge ();
System.out.println (age);
}
}
------ Solution ----------------------------------- ---------
can use the bottom ( 1 ) or ( 2 ) were realized . . . . . . .

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/ *
1) Create a List, an increase of three workers in the List , the basic information is as follows :
Name Age wages
zhang3 18 3000
li4 25 3500
wang5 22 3200
2) Before li4 insert a worker information : Name : zhao6, Age: 24 , salary 3300
3) Delete wang5 information
4) use for loop through and print information List of all workers
5) using an iterative traversal of the List of all the workers to call work methods.
6) Add equals method of the Worker class
6. (Set, Hash Algorithm ) for the previous question the Worker class, finished adding the equals method , based on adding an
hashCode methods. * /
public class TestDemo {

public static void main (String [] args) {
List al = new ArrayList ();
Person p1 = new Person (" Joe Smith ", 18 , 3000 ) ;
Person p2 = new Person (" John Doe ", 22 , 3500 ) ;
Person p3 = new Person (" King of Five" , 22, 3200 ) ;
Person p4 = new Person (" Zhao six " , 24,3300 ) ;
al.add (p1);
al.add (p2);
al.add (p3);
int s = al.indexOf (p2) ;/ / get the index value of John Doe
al.add (s, p4);
sop (" Zhao added after six " + al);
al.remove (p3);
sop (" After deleting the king five " + al);
Iterator it = al.iterator ();
/ / (1)
/ / for (; it.hasNext () ;) {
/ / sop (it.next ());
/ /}
/ / (2)
while (it.hasNext ()) {
Object obj = it.next ();
Person p = (Person) obj;
sop (p.getName () + p.work () + "" + p.getPrice () + p.money ());

}
}

public static void sop (Object sop) {
System.out.println (sop);
}

}

class Person {
private String name;
private int age;
private int price;

public Person (String name, int age, int price) {
this.name = name;
this.age = age;
this.price = price;
}

public String getName () {
return name;
}

public void setName (String name) {
this.name = name;
}

public int getAge () {
return age;
}

public void setAge (int age) {
this.age = age;
}

public int getPrice () {
return price;
}

public void setPrice (int price) {
this.price = price;
}

public String toString () {
return name + "" + age + "" + price;
}
public String work () {
return " at work "; / / System.out.println (" work " ) ;
}
public String money () {
return " wages "; / / System.out.println (" salary" ) ;
}

}
------ For reference only --------------------------------- ------
not for words with Iterator line does not , you certainly take the properties of each object to iterate , iterate certainly have to cycle almost ah :

Iterator ls=list.iterator();
while(iS.hasNext()){
       User us = iS.next();
       String age = us.getAge();
       System.out.println(age);
}

------ For reference only ----------------------------------- ----
wrong :

Iterator ite=ls.iterator();
while(ite.hasNext()){
       User us =  ite.next();
       String age = us.getAge();
       System.out.println(age);
}

------ For reference only ----------------------------------- ----
with Iterator can ;
------ For reference only -------------------------- -------------
also similar thing with what
------ For reference only ---------------- -----------------------
foreach can also traverse, I prefer to use foreach, convenient Introduction
------ For reference under this scenario only ---------------------------------------
more accustomed to using foreach

for( User user : ls ){
        System.out.println(user.getAge());
}

------ For reference only ----------------------------------- ----
for (User user: ls) 1.4 should not take . not getAge () , then you can use java reflection mechanism to value , using the framework of this approach in a multi-
------ For reference only ----------- ----------------------------
upstairs are all positive solution, which is pleasing to the eye with the landlord to see what it
- ---- For reference only ---------------------------------------
< br />

Oh, Blessings
------ For reference only ------------------------------ ---------


right, thank you

没有评论:

发表评论