Page 2 of 2 FirstFirst 12
Results 21 to 35 of 35
  1. #21
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,943
    Rep Power
    19

    Default

    The actual code the API says is:
    Java Code:
    int[] x = {length};
    Array.newInstance(componentType, x);
    You missed the second part, which is the call to the other newInstance() method that takes an array of parameters.

    ETA: Also, if you;re new to this, I really wouldn't recommend trawling through the stuff in the reflect package.
    Reflection is really not something a person new to Java should be looking at.

  2. #22
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    I stand corrected:

    I had commented out the import statement:

    Java Code:
     ----jGRASP exec: javac -g BegiProg.java
    
    BegiProg.java:25: cannot find symbol
    symbol  : variable Integer
    location: class BegiProg
    			Array.newInstance(Integer, x);
    			                  ^
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Your post send a crossed message. The text says I'm OK. The code shows an error?

    Here's a test I ran a while back:
    Java Code:
        Object anArray = java.lang.reflect.Array.newInstance(Class.forName("java.lang.String"), new int[4]);
        Object anArray2 = java.lang.reflect.Array.newInstance(String.class, 4);
        System.out.println("aA=" +anArray);  //aA=[[[[Ljava.lang.String;@187aeca
        System.out.println("aA2=" +anArray2);  //aA2=[Ljava.lang.String;@12dacd1

  4. #24
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    [QUOTE=Tolls;216861]The actual code the API says is:
    Java Code:
    int[] x = {length};
    Array.newInstance(componentType, x);
    You missed the second part, which is the call to the other newInstance() method that takes an array of parameters.[quote]

    I'm reading what you typed and it's not processing for some reason. So the first line (int[] x.....) is the declaration of the array and it's length? and the second line (Array.new....) is a method to create a new array. This method has two parameters: one component type, which I am still not sure what that is but for now, I'm assuming that is is the class corresponding to the primitive data types and x being the array declared in the first line? Is any of this correct?

    ETA: Also, if you;re new to this, I really wouldn't recommend trawling through the stuff in the reflect package.
    Reflection is really not something a person new to Java should be looking at.
    ETA? As in Estimated Time of Arrival? Not sure I get it.

    But as for the package, I'm learning and I wasn't going in that package on purpose, it just so happened that the Array class is in there, so that's how I ended there. I'm approaching Java by assuming that as long as I learn the syntax and how to navigate the API, I'll become more proficient faster, so I'm not too afraid of being a newbie in that package. If I can read it and make sense of it by knowing how to use the package and classes within it, I'll be ok.

  5. #25
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Your post send a crossed message. The text says I'm OK. The code shows an error?

    Here's a test I ran a while back:
    Java Code:
        Object anArray = java.lang.reflect.Array.newInstance(Class.forName("java.lang.String"), new int[4]);
        Object anArray2 = java.lang.reflect.Array.newInstance(String.class, 4);
        System.out.println("aA=" +anArray);  //aA=[[[[Ljava.lang.String;@187aeca
        System.out.println("aA2=" +anArray2);  //aA2=[Ljava.lang.String;@12dacd1
    I just tried running your code and I get the following error:
    Java Code:
     ----jGRASP exec: javac -g Test.java
    
    Test.java:4: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
    		    Object anArray = java.lang.reflect.Array.newInstance(Class.forName("java.lang.String"), new int[4]);
    		                                                                      ^
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.

  6. #26
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Tolls View Post
    The actual code the API says is:
    Java Code:
    int[] x = {length};
    Array.newInstance(componentType, x);
    You missed the second part, which is the call to the other newInstance() method that takes an array of parameters.

    ETA: Also, if you;re new to this, I really wouldn't recommend trawling through the stuff in the reflect package.
    Reflection is really not something a person new to Java should be looking at.
    ETA: Edited To ADD?

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I have a skeleton class I throw lines of code into to compile and test. Its main allows me not to add try{}catch:

    Java Code:
       public static void main(String[] args) throws Exception {

  8. #28
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Thank you. It works. That's a nifty addition. I have not yet encountered that in my reading, so thanks.

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    That's for Quick and dirty testing. Not for normal programming. I get dozens of pieces of code to compile and execute everyday so I like it easy.

  10. #30
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    Your post send a crossed message. The text says I'm OK. The code shows an error?

    Here's a test I ran a while back:
    Java Code:
        Object anArray = java.lang.reflect.Array.newInstance(Class.forName("java.lang.String"), new int[4]);
        Object anArray2 = java.lang.reflect.Array.newInstance(String.class, 4);
        System.out.println("aA=" +anArray);  //aA=[[[[Ljava.lang.String;@187aeca
        System.out.println("aA2=" +anArray2);  //aA2=[Ljava.lang.String;@12dacd1
    Oh by the way, how did you know to declare an object? Also, for the parameters that you added within the newInstance() method. Could you comment on that and tell me how you came up with that from the API's description? Thanks!

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I probably found it either by Googling or by trial and error. I tried all the combinations I could think of until it worked.

    I also have a couple of old books (vintage 1998) that have many excellent examples for all of the classes that existed back then.
    "The Java Class Libraries Second Edition" by Chan, Lee and Kramer. There are two volumes of 2000 pages each.
    Last edited by Norm; 06-21-2011 at 08:29 PM.

  12. #32
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    That's for Quick and dirty testing. Not for normal programming. I get dozens of pieces of code to compile and execute everyday so I like it easy.
    Got it. Thanks. I'll keep in my my programs and will comment it out until needed.

  13. #33
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Norm View Post
    I probably found it either by Googling or by trial and error. I tried all the combinations I could think of until it worked.
    Sounds good. Thanks.

  14. #34
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,943
    Rep Power
    19

    Default

    Quote Originally Posted by bigsonny View Post
    I'm reading what you typed and it's not processing for some reason. So the first line (int[] x.....) is the declaration of the array and it's length? and the second line (Array.new....) is a method to create a new array. This method has two parameters: one component type, which I am still not sure what that is but for now, I'm assuming that is is the class corresponding to the primitive data types and x being the array declared in the first line? Is any of this correct?
    The first line is declaring the parameter that is passed into the other newInstance() method. That other method takes an array of dimensions, since you might want to declare a new array which is [10][8][6]. The first method (the one with the length parameter) uses that to create an array with one element to pass into the second method to give you a one dimensional array.

    Again, if you are new to Java then I really wouldn't recommend looking at reflection.

    Quote Originally Posted by bigsonny View Post
    ETA? As in Estimated Time of Arrival? Not sure I get it.
    You got it in your follow up post. Edit To Add. Something stuck into a post after it had already been posted.


    Quote Originally Posted by bigsonny View Post
    But as for the package, I'm learning and I wasn't going in that package on purpose, it just so happened that the Array class is in there, so that's how I ended there. I'm approaching Java by assuming that as long as I learn the syntax and how to navigate the API, I'll become more proficient faster, so I'm not too afraid of being a newbie in that package. If I can read it and make sense of it by knowing how to use the package and classes within it, I'll be ok.
    That Array class is part of the reflection package. It doesn't represent a basic array. It's a bunch of utilities to allow you to use reflection with basic arrays.
    You would not normally need the stuff in there.

    Reflection is simply going to confuse you, or worse get you into the bad habit of using it everywhere.

  15. #35
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Got it. Thanks for taking the time to answer these questions.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. Replies: 2
    Last Post: 04-28-2009, 04:52 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •