Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Need help deserializing an integer array.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help deserializing an integer array.

    I'm not sure how I should deserialize multi-dimensional arrays in Java. I'm given something like:

    <var object="[[I">
    	<_0 object="[I">
    		<_0 object="java.lang.Integer">1</_0>
    		<_1 object="java.lang.Integer">2</_1>
    	</_0>
    	<_1 object="[I">
    		<_0 object="java.lang.Integer">3</_0>
    		<_1 object="java.lang.Integer">4</_1>
    	</_1>
    </var>

    Where the object attribute on each node describes what object it is. If it is an array of a primitive type, it will begin with an array of '[', where each one represents a count in its depth. I.e:
    type int[][] = [[I
    type int = [i

    The problem is, how to I create an array of variable depth? Sorry if it isn't clear, I'm really confused by this problem :S


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help deserializing an integer array.

    2 ways:

    1. Manually create a larger array as needed, copying over all the old elements and then deleting the old array.
    2. Pre-read the data and get a fixed count of the data set you're reading in.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help deserializing an integer array.

    I thought I may need to do something like that; the problem is I don't know how I'd programatically create an array with a depth of x? I.e, if I had to do it my way it would probably be something like:

    switch(iArrayDepth)
    {
        case 1:
            return new ArrayList<Integer>();
        case 2:
            return new ArrayList<ArrayList<Integer>>();
        case 3:
            return new ArrayList<ArrayList<ArrayList<Integer>>>();
    etc...
    }
    There must be a better way of doing it :S

    Another problem arises when I need to unbox this array and make it a mutlidimensional array of a primitive type.
    Last edited by jeremy_; May 29th, 2011 at 12:54 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help deserializing an integer array.

    hmm, I see your point.

    Unfortunately, the Only way I can think of to create an x-dimension array is to use reflection, which means no compile-time type checking.

    Object array = Array.newInstance(Integer.class, firstDimensionSize, secondDimensionSize, thirdDimensionSize); // first, second, and third dimension size are intengers. You can have as many or few dimensions as you want

    Does your input data have to be in this format? If it doesn't, I would recommend changing the file to use the default Serialize and De-serializing mechanism provided by Java for arrays, then use an ObjectOutputStream to write out to the file and an ObjectInputStream to read in from the file.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help deserializing an integer array.

    how do I create an array of variable depth
    Not sure what you mean here. You imply that you don't know the number of dimensions until execution time.
    If you were able to create this array at execution time, how would your code use/access it?
    Perhaps by a method with an arraylist for argument. There would be an entry in the arraylist for each dimension. The underlying data would be in a single dimension array. Somewhere there would need to be the max number of elements for each dimension.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help deserializing an integer array.

    If I understand the requirements - an array of arrays in which you don't know the size until runtime - it sounds like you want some sort of data structure, for example a linked list, tree, or graph which can then be initialized recursively and structure via references which can be dynamically changed/removed/initialized at runtime.

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Need help deserializing an integer array.

    You can do this with the Array class. It is part of the reflection package, but it is designed for just this kind of thing, and it does create real arrays (although they're handled as Objects).

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help deserializing an integer array.

    The new thing learned today:
        Object anArray = java.lang.reflect.Array.newInstance(Class.forName("java.lang.String"), new int[4]);
        System.out.println("aA=" +anArray);  //aA=[[[[Ljava.lang.String;@187aeca\

  9. #9
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Need help deserializing an integer array.

    Why make it complicated, why not just:
    import java.lang.reflect.Array;
    ...
    Object anArray = Array.newInstance(String.class, 4);

    ETA - oh wait, I was forgetting, you need the Class.forName(..) if deserializing a text representation... forget I said that
    Last edited by dlorde; May 29th, 2011 at 07:58 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help deserializing an integer array.

    You get different results with that:
        Object anArray2 = java.lang.reflect.Array.newInstance(String.class, 4);
        System.out.println("aA2=" +anArray2);  //aA2=[Ljava.lang.String;@12dacd1
        String[] aA2S = (String[])anArray2;
        System.out.println("aA2S.length=" + aA2S.length); //aA2S.length=4
    One dimensional with 4 elements

  11. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Need help deserializing an integer array.

    Quote Originally Posted by Norm View Post
    One dimensional with 4 elements
    OIC - oops, my bad - I didn't look too closely at the API, but should have noticed the difference in parameters.

    It was late, I was tired, what can I say? OTOH maybe I should get more sleep...

Similar Threads

  1. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 09:13 PM
  2. Help with ArrayList<Integer>
    By shanklove in forum Collections and Generics
    Replies: 2
    Last Post: September 6th, 2010, 09:15 AM
  3. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM
  4. Hex to Integer Problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 06:01 AM
  5. int and Integer
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 31st, 2009, 03:20 AM