導航:首頁 > 編程語言 > java遍歷對象的屬性

java遍歷對象的屬性

發布時間:2022-05-16 16:50:40

『壹』 java如何遍歷對象數組

for (int i = 0; i < 2; i++) {
Customer cust = new Customer();// 屬性對象
System.out.print("輸入會員編號:");
cust.number = input.nextInt();
System.out.print("輸入會員積分:");
cust.integral = input.nextInt();
cust2.add(cust);// 傳入對象參數
}
每次循環該創建新的對象,你操作的是同一個Customer,所以值會被覆蓋掉

『貳』 java中」遍歷「,」迭代「是什麼意思

我認為迭代是遍歷的一種吧,遍歷是查找的意思吧
迭代器模式(Iterator pattern)
一、 引言
迭代這個名詞對於熟悉Java的人來說絕對不陌生。我們常常使用JDK提供的迭代介面進行java collection的遍歷:
Iterator it = list.iterator();
while(it.hasNext()){
//using 「it.next();」do some businesss logic
}
而這就是關於迭代器模式應用很好的例子。
二、 定義與結構
迭代器(Iterator)模式,又叫做游標(Cursor)模式。GOF給出的定義為:提供一種方法訪問一個容器(container)對象中各個元素,而又不需暴露該對象的內部細節。
從定義可見,迭代器模式是為容器而生。很明顯,對容器對象的訪問必然涉及到遍歷演算法。你可以一股腦的將遍歷方法塞到容器對象中去;或者根本不去提供什麼遍歷演算法,讓使用容器的人自己去實現去吧。這兩種情況好像都能夠解決問題。
然而在前一種情況,容器承受了過多的功能,它不僅要負責自己「容器」內的元素維護(添加、刪除等等),而且還要提供遍歷自身的介面;而且由於遍歷狀態保存的問題,不能對同一個容器對象同時進行多個遍歷。第二種方式倒是省事,卻又將容器的內部細節暴露無遺。
而迭代器模式的出現,很好的解決了上面兩種情況的弊端。先來看下迭代器模式的真面目吧。
迭代器模式由以下角色組成:
1) 迭代器角色(Iterator):迭代器角色負責定義訪問和遍歷元素的介面。
2) 具體迭代器角色(Concrete Iterator):具體迭代器角色要實現迭代器介面,並要記錄遍歷中的當前位置。
3) 容器角色(Container):容器角色負責提供創建具體迭代器角色的介面。
4) 具體容器角色(Concrete Container):具體容器角色實現創建具體迭代器角色的介面——這個具體迭代器角色於該容器的結構相關。
迭代器模式的類圖如下:

從結構上可以看出,迭代器模式在客戶與容器之間加入了迭代器角色。迭代器角色的加入,就可以很好的避免容器內部細節的暴露,而且也使得設計符號「單一職責原則」。
注意,在迭代器模式中,具體迭代器角色和具體容器角色是耦合在一起的——遍歷演算法是與容器的內部細節緊密相關的。為了使客戶程序從與具體迭代器角色耦合的困境中脫離出來,避免具體迭代器角色的更換給客戶程序帶來的修改,迭代器模式抽象了具體迭代器角色,使得客戶程序更具一般性和重用性。這被稱為多態迭代。
三、 舉例
由於迭代器模式本身的規定比較鬆散,所以具體實現也就五花八門。我們在此僅舉一例,根本不能將實現方式一一呈現。因此在舉例前,我們先來列舉下迭代器模式的實現方式。
1.迭代器角色定義了遍歷的介面,但是沒有規定由誰來控制迭代。在Java collection的應用中,是由客戶程序來控制遍歷的進程,被稱為外部迭代器;還有一種實現方式便是由迭代器自身來控制迭代,被稱為內部迭代器。外部迭代器要比內部迭代器靈活、強大,而且內部迭代器在java語言環境中,可用性很弱。
2.在迭代器模式中沒有規定誰來實現遍歷演算法。好像理所當然的要在迭代器角色中實現。因為既便於一個容器上使用不同的遍歷演算法,也便於將一種遍歷演算法應用於不同的容器。但是這樣就破壞掉了容器的封裝——容器角色就要公開自己的私有屬性,在java中便意味著向其他類公開了自己的私有屬性。
那我們把它放到容器角色里來實現好了。這樣迭代器角色就被架空為僅僅存放一個遍歷當前位置的功能。但是遍歷演算法便和特定的容器緊緊綁在一起了。
而在Java Collection的應用中,提供的具體迭代器角色是定義在容器角色中的內部類。這樣便保護了容器的封裝。但是同時容器也提供了遍歷演算法介面,你可以擴展自己的迭代器。
好了,我們來看下Java Collection中的迭代器是怎麼實現的吧。
//迭代器角色,僅僅定義了遍歷介面
public interface Iterator {
boolean hasNext();
Object next();
void remove();
}
//容器角色,這里以List為例。它也僅僅是一個介面,就不羅列出來了
//具體容器角色,便是實現了List介面的ArrayList等類。為了突出重點這里指羅列和迭代器相關的內容
//具體迭代器角色,它是以內部類的形式出來的。AbstractList是為了將各個具體容器角色的公共部分提取出來而存在的。
public abstract class AbstractList extends AbstractCollection implements List {
……
//這個便是負責創建具體迭代器角色的工廠方法
public Iterator iterator() {
return new Itr();
}
//作為內部類的具體迭代器角色
private class Itr implements Iterator {
int cursor = 0;
int lastRet = -1;
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public Object next() {
checkForComodification();
try {
Object next = get(cursor);
lastRet = cursor++;
return next;
} catch(IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch(IndexOutOfBoundsException e) {
throw new ();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ();
}
}
至於迭代器模式的使用。正如引言中所列那樣,客戶程序要先得到具體容器角色,然後再通過具體容器角色得到具體迭代器角色。這樣便可以使用具體迭代器角色來遍歷容器了……
四、 實現自己的迭代器
在實現自己的迭代器的時候,一般要操作的容器有支持的介面才可以。而且我們還要注意以下問題:
在迭代器遍歷的過程中,通過該迭代器進行容器元素的增減操作是否安全呢?
在容器中存在復合對象的情況,迭代器怎樣才能支持深層遍歷和多種遍歷呢?
以上兩個問題對於不同結構的容器角色,各不相同,值得考慮。
五、 適用情況
由上面的講述,我們可以看出迭代器模式給容器的應用帶來以下好處:
1) 支持以不同的方式遍歷一個容器角色。根據實現方式的不同,效果上會有差別。
2) 簡化了容器的介面。但是在java Collection中為了提高可擴展性,容器還是提供了遍歷的介面。
3) 對同一個容器對象,可以同時進行多個遍歷。因為遍歷狀態是保存在每一個迭代器對象中的。
由此也能得出迭代器模式的適用范圍:
1) 訪問一個容器對象的內容而無需暴露它的內部表示。
2) 支持對容器對象的多種遍歷。
3) 為遍歷不同的容器結構提供一個統一的介面(多態迭代)。

『叄』 java中有沒有辦法直接遍歷Object對象,將Object中的各屬性值輸出,如何進行

你可以藉助泛型類Iterator<E>將Objiect對象遍歷。具體的實現去查查jad1.6 API函數吧。如果沒有,給郵箱

『肆』 Java:在JSP頁面中怎麼遍歷Session中的集合里對象的屬性 Session中放的是一個List<Student>

<c:forEach var="aa" items="a"><!-- a是你session傳過來的集合 -->
<c:out value="${aa.id}"></c:out>
<c:out value="${aa.name}"></c:out>
</c:forEach>

『伍』 java 遍歷 類屬性的屬性值

用反射,代碼你可以參考下面的:
public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Field[] field = model.getClass().getDeclaredFields(); //獲取實體類的所有屬性,返回Field數組
for(int j=0 ; j<field.length ; j++){ //遍歷所有屬性
String name = field[j].getName(); //獲取屬性的名字

System.out.println("attribute name:"+name);
String type = field[j].getGenericType().toString(); //獲取屬性的類型
if(type.equals("class java.lang.String")){ //如果type是類類型,則前麵包含"class ",後面跟類名
Method m = model.getClass().getMethod("get" + name.substring(0,1).toUpperCase() + name.substring(1) );
String value = (String) m.invoke(model); //調用getter方法獲取屬性值
if(value != null){

System.out.println("attribute value:"+value);
}
}
if(type.equals("class java.lang.Integer")){
Method m = model.getClass().getMethod("get" + name.substring(0,1).toUpperCase() + name.substring(1) );
Integer value = (Integer) m.invoke(model);
if(value != null){
System.out.println("attribute value:"+value);
}
}
if(type.equals("class java.lang.Short")){
Method m = model.getClass().getMethod("get" + name.substring(0,1).toUpperCase() + name.substring(1) );
Short value = (Short) m.invoke(model);
if(value != null){
System.out.println("attribute value:"+value); }
}
if(type.equals("class java.lang.Double")){
Method m = model.getClass().getMethod("get" + name.substring(0,1).toUpperCase() + name.substring(1) );
Double value = (Double) m.invoke(model);
if(value != null){
System.out.println("attribute value:"+value);
}
}
if(type.equals("class java.lang.Boolean")){
Method m = model.getClass().getMethod("get"+name);
Boolean value = (Boolean) m.invoke(model);
if(value != null){
System.out.println("attribute value:"+value);
}
}
if(type.equals("class java.util.Date")){
Method m = model.getClass().getMethod("get"+name);
Date value = (Date) m.invoke(model);
if(value != null){
System.out.println("attribute value:"+value.toLocaleString());
}
}
}

『陸』 java中如何遍歷實體類的屬性和數據類型以及

可以通過反射獲取到屬性方法信息:

publicclassUser{
privateintid;
privateStringname;
privatedoubleamount;

publicintgetId(){
returnid;
}

publicvoidsetId(intid){
this.id=id;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicdoublegetAmount(){
returnamount;
}

publicvoidsetAmount(doubleamount){
this.amount=amount;
}
}

獲取類信息:

importjava.lang.reflect.Field;
importjava.lang.reflect.Method;
importjava.lang.reflect.Parameter;

publicclassReflectDemo{
publicstaticvoidmain(String[]args){
Classclz=User.class;
//獲取所有自己定義的屬性(不包含從Object繼承的,如果需要可使用getFields()方法)
Field[]fields=clz.getDeclaredFields();
for(Fieldf:fields){
System.out.println("屬性名稱:"+f.getName()+",屬性類型:"+f.getType().getName());
}

//獲取所有自己定義的方法(同樣不包含繼承的)
Method[]methods=clz.getDeclaredMethods();
for(Methodm:methods){
System.out.println("方法名稱:"+m.getName());
System.out.println("返回值類型:"+m.getReturnType().getName());
//獲取方法的所有參數
Parameter[]parameters=m.getParameters();
for(Parameterp:parameters){
System.out.println("參數類型:"+p.getType().getName());
}
System.out.println("==========================================");
}
}
}

『柒』 java中如何遍歷一個類的所有對象

你這里的numbers是一個對象數組,所以你可以這樣遍歷,單個對象是不行的。遍歷對象內部成員,在反射裡面有方法,我剛練習完,只有將對象的成員分解到數組中才行。分享給你了:
Class c=Class.forName("AbstractClassTest.Car"); //要包名+類名
Object o=c.newInstance();
Car car=(Car)o;
Field[] fields=c.getDeclaredFields();//拿到數據成員
Method[] methods=c.getMethods();//拿到函數成員
/*System.out.println(fields.length);
System.out.println(methods.length);*/
for(Field f : fields){
System.out.println("該類的內部變數有:"+f.getName());
}
for(Method m : methods) {
System.out.println("該類的方法有:"+m.getName());
}

『捌』 java List遍歷取值(一個對象一個屬性,取出放置另一個對象7個屬性)

在B對象里定義一個A對象的List<A>

jsp 添加jstl標簽引用 即可

<c:forEach items="${B.list}" var="A">
<tr>
<td>${A.id}</td>
</tr>
</c:forEach>

『玖』 Java相關,求教一個問題,如何遍歷出list里的對象,然後調用那個對象的方法

importjava.util.*;

//測試方法
publicclassMain{
publicstaticvoidmain(String[]args){
Studentstudent1=newStudent();
student1.setName("張三");

Studentstudent2=newStudent();
student2.setName("李四");

Studentstudent3=newStudent();
student3.setName("王五");

List<Student>list=newArrayList<Student>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);

for(Studentstudent:list){
System.out.println(student.getName());
}

//或
//for(inti=0;i<list.size;i++){
//System.out.println(list.get(i).getName());
//}
}
}

//Student實體類
publicclassStudent{
privateStringname;

publicStudent(){

}

publicvoidsetName(Stringname){
this.name=name;
}

publicStringgetName(){
returnname;
}
}

『拾』 java 如何實現判斷一個對象所有的屬性是否為空

其實不用那麼麻煩,只用定義一個方法,然後使用下面的代碼片段來判斷欄位是否為空:

for (Field f : obj.getClass().getDeclaredFields()) {
f.setAccessible(true);
if (f.get(obj) == null) { //判斷欄位是否為空,並且對象屬性中的基本都會轉為對象類型來判斷
......
}
}

閱讀全文

與java遍歷對象的屬性相關的資料

熱點內容
鉸刀轉速進給的演算法 瀏覽:977
php二維數組取一列 瀏覽:375
安裝殺毒軟體出現壓縮或加密 瀏覽:973
方舟端游伺服器怎麼搜索房間 瀏覽:71
單片機學51好還是stm8好 瀏覽:798
手中的app如何隱藏 瀏覽:1001
安卓什麼壁紙軟體號 瀏覽:436
java設置內存大小 瀏覽:434
php循環匹配 瀏覽:325
技巧pdf 瀏覽:481
單片機斷程序怎麼解決 瀏覽:160
如何製作APP的圖片 瀏覽:506
php大小排序 瀏覽:550
linuxkerberos 瀏覽:126
暗黑破壞神3如何下載亞洲伺服器 瀏覽:953
linux中ftp伺服器地址怎麼看 瀏覽:438
ansys命令流do 瀏覽:122
單片機6502 瀏覽:765
自助洗車有什麼app 瀏覽:937
程序員離職率多少 瀏覽:322