• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

delete an element from an array

 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a practice example for my test that i cannot figure out. Please help

Question:
Write a method deleteElement which takes as input an int[] and an int target and deletes all
occurrences of target from the array. The method should return the new int[]. Question to consider:
Why is it that we have to return an array and can't simply change the input parameter array?

this is the code that ive come up with but I know its wrong. The problem with it is that it does not take into consideration whether or not the array[i+1] is equal to the target number or not.

 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:
Write a method deleteElement which takes as input an int[] and an int target and deletes all
occurrences of target from the array. The method should return the new int[]. Question to consider:
Why is it that we have to return an array and can't simply change the input parameter array?


Array size is constant and so cannot be changed. You cannot remove an element from an array. you can modify it though. So you have to create a new Array and put all the elements except the target element and return this new array. Where as with array lists or any lists you can return the input parameter array list by removing the element using .remove() method. The size of list changes dynamically.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you mean in an array of primitives, you can only replace an element.
I am changing your code tags to quote, which is more appropriate.
 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt realize what an array list was. I thought it was the exact same thing as an array. Im gonna work on my code and I think I will have a better solution.
 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you create an array list?

I read that the array list initializes with a value of 10. Is this true?

thanks
 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie,

What do you mean that you are changing code tags to quotes?

Im new this forum so I want to understand how it works.

Thank you
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe Campbell was talking to Harsha Smith. I didn't see it, but I'm guessing that post originally hay the quotation you see now in 'code' tags, which would just look odd.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Fred, HS had used code tags, where quote would have been more appropriate. Sorry for any misunderstanding.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark donner wrote:How do you create an array list?

I read that the array list initializes with a value of 10. Is this true?

thanks


Mark, you can find the JavaDoc for ArrayList here.

ArrayList is a concrete implementation of the List interface, so if you want to follow best practices, you would probably want to do something like this:

where Type is a class type (that is, a non-primitive).

To answer your second question, yes, unless you specify a different initial capacity, an ArrayList will have the default initial capacity of 10.

John.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic