Thread: Java hw help

Results 1 to 8 of 8
  1. #1 Java hw help 
    Registered Member
    Join Date
    Mar 2017
    Posts
    29
    Thanks given
    1
    Thanks received
    0
    Rep Power
    11
    Code:
    int[] deleteAt(int[] nums, int index) {
      
      int[] nums2 = new int[nums.length-1];
      int j = 0;
      
      
      
        for(int i = 0; i < nums.length-1; i++){
            
            if(i == index) {
                i++;
            }
            
            
            
            nums2[j++] = nums[i];
            
         
            
           
            
        }
    
        return nums2;
      
    }

    Expected Run
    deleteAt([1, 2, 3], 0) → [2, 3] [2, 0] X - what should i do to fix this??
    deleteAt([4, 5, 6], 1) → [4, 6] [4, 6] OK
    deleteAt([9, 8, 7, 6, 5, 4], 4) → [9, 8, 7, 6, 4] [9, 8, 7, 6, 4] OK
    deleteAt([5], 0) → [] [] OK
    deleteAt([18, 45, 13, 27, 42], 4) → [18, 45, 13, 27] [18, 45, 13, 27] OK
    Reply With Quote  
     

  2. #2  
    I need some more coffee

    Jason's Avatar
    Join Date
    Aug 2009
    Age
    24
    Posts
    6,018
    Thanks given
    2,283
    Thanks received
    2,697
    Rep Power
    3634
    What
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Mar 2017
    Posts
    29
    Thanks given
    1
    Thanks received
    0
    Rep Power
    11
    the expected run basically runs the test, can I get some help.
    Reply With Quote  
     

  4. #4  
    Registered Member
    Join Date
    Jul 2012
    Age
    20
    Posts
    115
    Thanks given
    13
    Thanks received
    20
    Rep Power
    15
    I believe what happens is that it never sets the value to 3. I would change the condition to use j's value instead. You're ultimately incrementing i too fast.

    Code:
    int[] deleteAt(int[] nums, int index) {
      
      int[] nums2 = new int[nums.length-1];
      int i = 0;
      
      
      
        for(int j = 0; j < nums2.length; j++){
            
            if(i == index) {
                i++;
            }
            
            
            
            nums2[j] = nums[i];
            
         
            i++;
           
            
        }
    
        return nums2;
      
    }
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Mar 2017
    Posts
    29
    Thanks given
    1
    Thanks received
    0
    Rep Power
    11
    Quote Originally Posted by Sarkastisk View Post
    I believe what happens is that it never sets the value to 3. I would change the condition to use j's value instead. You're ultimately incrementing i too fast.

    Code:
    int[] deleteAt(int[] nums, int index) {
      
      int[] nums2 = new int[nums.length-1];
      int i = 0;
      
      
      
        for(int j = 0; j < nums2.length; j++){
            
            if(i == index) {
                i++;
            }
            
            
            
            nums2[j] = nums[i];
            
         
            i++;
           
            
        }
    
        return nums2;
      
    }
    Although it is not the best practice I believe it should work.

    Expected Run
    deleteAt([1, 2, 3], 0) → [2, 3] ArrayIndexOutOfBoundsException: 3 (line:16) X
    deleteAt([4, 5, 6], 1) → [4, 6] ArrayIndexOutOfBoundsException: 4 (line:16) X
    deleteAt([9, 8, 7, 6, 5, 4], 4) → [9, 8, 7, 6, 4] ArrayIndexOutOfBoundsException: 7 (line:16) X
    deleteAt([5], 0) → [] ArrayIndexOutOfBoundsException: 1 (line:16) X
    deleteAt([18, 45, 13, 27, 42], 4) → [18, 45, 13, 27] ArrayIndexOutOfBoundsException: 5 (line:16) X


    Reply With Quote  
     

  6. #6  
    Registered Member
    Join Date
    Jul 2012
    Age
    20
    Posts
    115
    Thanks given
    13
    Thanks received
    20
    Rep Power
    15
    Quote Originally Posted by Eddies View Post
    Expected Run
    deleteAt([1, 2, 3], 0) → [2, 3] ArrayIndexOutOfBoundsException: 3 (line:16) X
    deleteAt([4, 5, 6], 1) → [4, 6] ArrayIndexOutOfBoundsException: 4 (line:16) X
    deleteAt([9, 8, 7, 6, 5, 4], 4) → [9, 8, 7, 6, 4] ArrayIndexOutOfBoundsException: 7 (line:16) X
    deleteAt([5], 0) → [] ArrayIndexOutOfBoundsException: 1 (line:16) X
    deleteAt([18, 45, 13, 27, 42], 4) → [18, 45, 13, 27] ArrayIndexOutOfBoundsException: 5 (line:16) X


    If you copied the code before quoting me try again. I did some changes. I'm on my phone atm so I was a bit sloppy.
    Reply With Quote  
     

  7. Thankful user:


  8. #7  
    Registered Member
    Join Date
    Mar 2017
    Posts
    29
    Thanks given
    1
    Thanks received
    0
    Rep Power
    11
    Quote Originally Posted by Sarkastisk View Post
    If you copied the code before quoting me try again. I did some changes. I'm on my phone atm so I was a bit sloppy.
    Thank you so much.
    Reply With Quote  
     

  9. #8  
    Registered Member Warnen's Avatar
    Join Date
    Jun 2018
    Posts
    9
    Thanks given
    0
    Thanks received
    3
    Rep Power
    0
    Glad you got help from someone.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Java HW Help
    By tereve in forum Homework
    Replies: 5
    Last Post: 11-16-2012, 12:14 PM
  2. Java download help
    By Xxxpearsonz in forum Help
    Replies: 5
    Last Post: 06-14-2009, 11:53 AM
  3. Java Application help plz!
    By T9X in forum Help
    Replies: 3
    Last Post: 04-09-2009, 01:38 AM
  4. [508] server.java compile help
    By 1kk4 in forum Help
    Replies: 10
    Last Post: 02-22-2009, 08:14 PM
  5. Replies: 6
    Last Post: 01-16-2009, 10:26 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •