This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

delete an element from an array  RSS feed

 
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.

 
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.
 
Marshal
Posts: 60192
188
  • 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
 
lowercase baba
Bartender
Posts: 12627
50
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
Marshal
Posts: 60192
188
  • 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.
 
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
Boost this thread!