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 10 of 10

Thread: Generic Array Tools (lend me a critical eye)

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Generic Array Tools (lend me a critical eye)

    Is there a problem with this conceptually - as in do you foresee any issues?

    /*
    *  Trying to make a universal tool for increment an array by one while keeping all the previous values in place.
    * 
    */
                    public K[] increment(K[] k){
    		int i = 0;
    		K[] tmp = (K[])new Object[Array.getLength(k)+1]; 
    /*
    * Parses through the passed k and fills tmp with all of ks values
    *                
    */
    		for(K kk: k){
    			tmp[i] = kk;
    			++i;
    					}
    					return tmp;
    				    }

    Consequently then this.

    /*
    *  Same with the first one, but for 2D arrays.
    *
    */
    	public K[][] increment(K[][] k){
    		int i = 0;
    		int ii = 0;
    		K[][] tmp = (K[][])new Object[Array.getLength(k)+1][];
    		for(K[] kara: k){
     
    //          instantiate a new array at i with the length of the current iteration ( kara )
     
    			tmp[i] = (K[])new Object[Array.getLength(kara)];
    			for(K kay: kara){
     
       //              fill tmp's array at i with the same values of kara
     
    				tmp[i][ii]=kay;
    				++ii;
    							}
    							++i;
    							ii=0;
    						}	
    							return tmp;	
    								   }
    Last edited by KAJLogic; February 28th, 2014 at 01:23 PM.


  2. #2
    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: Generic Array Tools (lend me a critical eye)

    What is the code supposed to do? Add some comments describing what it does and any problems it had to solve to get it to work.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Generic Array Tools (lend me a critical eye)

    Apologies I did that. while I was looking at it I noticed I probably could have for the 2D array simply made tmp[i] = kara.

     
     
     
     
    /*
    *  Same with the first one, but for 2D arrays.
    *
    */
    	public K[][] increment(K[][] k){
    		int i = 0;
    		K[][] tmp = (K[][])new Object[Array.getLength(k)+1][];
    		for(K[] kara: k){
     
    //          just make tmp[i] = kara iteration 
    // essentially use Java's built in array copy tool
    			tmp[i] = kara;
    							++i;
     
    						}

    The only thing that makes me a little uneasy about this is my lack of knowledge on how an array is copied by the API implementation.

  4. #4
    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: Generic Array Tools (lend me a critical eye)

    The Arrays class has methods that do some of that more efficiently.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Generic Array Tools (lend me a critical eye)

    which part?

  6. #6
    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: Generic Array Tools (lend me a critical eye)

    The copy part
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    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: Generic Array Tools (lend me a critical eye)

    I'm not sure about the goal here, but if you are wishing for a more dynamic array perhaps use a List. Alternatively you can make your methods more flexible by adding a parameter which allows one to expand the array by a given value (rather than 1 every time).

    Also see System (Java Platform SE 7 )

    And lastly, are you looking to make a copy of the array, or reference the same (your 2D array references...and it should be noted in the comments that it does this - thus changes in the parameter array affect the returned array)

  8. #8
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Generic Array Tools (lend me a critical eye)

    Can you elaborate on your lastly; where does it reference?

    P.S: your first suggestion is excellent I will add that.

  9. #9
    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: Generic Array Tools (lend me a critical eye)

    Quote Originally Posted by KAJLogic View Post
    Can you elaborate on your lastly; where does it reference?
    Consider the following - an adaptation of your 2D array code:
    public static void main(String[] args){
    	int[][] a = {{1,2},{3,4}};
    	int[][] b = new int[a.length+1][];
    	for ( int i = 0; i < a.length; i++ ){
    		b[i] = a[i];
    	}
     
    	System.out.println(Arrays.toString(a[0]));
    	System.out.println(Arrays.toString(b[0]));
    	a[0][0] = 10000;
    	System.out.println(Arrays.toString(a[0]));
    	System.out.println(Arrays.toString(b[0]));		
    }
    /*
    prints:
    [1, 2]
    [1, 2]
    [10000, 2]
    [10000, 2]
     
    */
    Note the above code changes a value within the a array, which also changes the value in array b because b[n] references a[n] rather than copies. Whether this is the behavior you seek I do not know, but it should be explicit in the comments. You might be better off getting explicit advice (or advice for easier alternatives) to expand upon what your goal/requirement(s) actually is

  10. #10
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: Generic Array Tools (lend me a critical eye)

    /*
    *  Same with the first one, but for 2D arrays.
    *
    */
    	public K[][] increment(K[][] k){
    		int i = 0;
    		int ii = 0;
    		K[][] tmp = (K[][])new Object[Array.getLength(k)+1][];
    		for(K[] kara: k){
     
    //          instantiate a new array at i with the length of the current iteration ( kara )
     
    			tmp[i] = (K[])new Object[Array.getLength(kara)];
    			for(K kay: kara){
     
       //              fill tmp's array at i with the same values of kara
     
    				tmp[i][ii]=kay;
    				++ii;
    							}
    							++i;
    							ii=0;
    						}	
    							return tmp;	
    								   }

    In my program I often find myself adding entires to arrays but of different types. I want to add this to my utility class, so I can just instantiate the type using a generic and increment my arrays with ease. I do not need to refrence each array in the parameter 2D array just copy so I can return an array exactly the same as the one in the paremeter, but the length should be greater by one.
    I instantiate after the method.

Similar Threads

  1. Replies: 2
    Last Post: October 19th, 2012, 03:32 AM
  2. having problem declaring a generic type array in OrderSet class
    By mia_tech in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 10th, 2012, 06:39 PM
  3. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 09:22 PM