• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

delete an element from an array

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Fred, HS had used code tags, where quote would have been more appropriate. Sorry for any misunderstanding.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic