The Artima Developer Community
Sponsored Link

Java Answers Forum
trying to check the length of an array with is a member of a generic class

1 reply on 1 page. Most recent reply: May 26, 2008 4:05 AM by Albrecht Mueller

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Albrecht Mueller

Posts: 4
Nickname: lbrtchx
Registered: Feb, 2003

trying to check the length of an array with is a member of a generic class Posted: May 26, 2008 4:00 AM
Reply to this message Reply
Advertisement
Array.getLength(?) doesn't seem to be working for me
~
Here is a piece of demo code I quickly scratch
~
import java.util.*;
import java.lang.reflect.*;

// __
class KAI{
public String a;
public int i;
public long[] lAr;
public byte[] bAr;

}

// __
class KAX{
private String a00;
private final String a04 = "04";
private final int[] iAr = new int[]{0, 1, 2};

public long l00;
public String[][] sSAr = new String[][]{{"a","b"},{"0","1","2"}};
public ArrayList<String> ALS;
KAI[] KAIAr = new KAI[2];

}

// __
class KFlds00{
KFlds00(){}

// __
public void printKFieldsAttrs(Object Obj){
// __
try{
Field Flds[] = (Obj.getClass()).getDeclaredFields();
for (int j = 0; j < Flds.length; ++j){
Flds[j].setAccessible(true);
System.out.println("// __ Flds[" + j + "]: |" + Flds[j].getName()
+ "|, |" + Flds[j].getModifiers() + "|" +
Modifier.toString(Flds[j].getModifiers()) + "|" +
Flds[j].getDeclaringClass() + "|");
// __
Class KTp = Flds[j].getType();
if (KTp.isArray()) {
Class CompTp = KTp.getComponentType();
System.out.println("// __ Array of: |" + CompTp + "|");
System.out.println("// __ isPrimitive: |" + CompTp.isPrimitive() + "|");

// __ this below is what does not work!

// System.out.println("// __ Array size: |" + Array.getLength(?) + "|");
}
else{
System.out.println("// __ " + KTp + "|" + Flds[j].get(Obj) + "|");
}
}
}catch(InstantiationException InstX){ InstX.printStackTrace(); }
catch(IllegalAccessException IlgAxX){ IlgAxX.printStackTrace(); }
// __
}

}

// __
public class KFlds00Test{
public static void main(String[] aArgs){
KAI K = new KAI();
KFlds00 KFlds = new KFlds00();
KFlds.printKFieldsAttrs(K);
// __
KAX KX = new KAX();
KFlds.printKFieldsAttrs(KX);
}
}


Albrecht Mueller

Posts: 4
Nickname: lbrtchx
Registered: Feb, 2003

trying to check the length of an array WHICH is a class member Posted: May 26, 2008 4:05 AM
Reply to this message Reply
the class isn't actually generic
~
this is what I will have to do next

Flat View: This topic has 1 reply on 1 page
Topic: Java Debug Level Previous Topic   Next Topic Topic: Places Bar

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use