Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Program for inserting and deleting element(s)?  RSS feed

 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to learn arrays in java and I have to do this program that has an array or a stack that holds a fixed amount of 10 and is a type array. The program allows you to insert and extract an element to/from it through methods, one for inserting and one for deleting.
Should i use normal array? arraylists.. collections?

I'm not quite sure how to do it?
Any help? I'm not totally asking for the code but the logic, i'm bad at logic..
 
Stephan van Hulst
Saloon Keeper
Posts: 7807
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the exact problem description?

Normally, I would do this using a type of List, such as ArrayList, but this sounds like an assignment in which you're only allowed to use arrays.

You should start out by writing a class that encapsulates an array and add methods to it that interact with the array.

Show us what you've tried so far, and we can offer advice when you're stuck.
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Do you have the exact problem description?

Normally, I would do this using a type of List, such as ArrayList, but this sounds like an assignment in which you're only allowed to use arrays.


Not exactly an assignment, just an exercise for myself..
I'm not bounded to using just normal arrays, i can use arraylist but doesn't arraylist grow? I need a fixed size which is 10.

I want to create method for inserting and one for extracting.. i'm not sure how to insert or extract elements from an array..

And the description is exactly what i have written.
 
Stephan van Hulst
Saloon Keeper
Posts: 7807
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but they already have methods for insertion and deletion. You can wrap your own class around an ArrayList, and just check the size constraints before you try to add elements to the list.
 
Campbell Ritchie
Marshal
Posts: 55711
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use an array, note these two methods:- 1 2.
I think you will find No 2 easier to use; you can probably part of an array into itself.
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan Crandego wrote:
I'm trying to learn arrays in java and I have to do this program that has an array or a stack that holds a fixed amount of 10 and is a type array. The program allows you to insert and extract an element to/from it through methods, one for inserting and one for deleting.
Should i use normal array? arraylists.. collections?

Since this a learning exercise for you, why not try using both and see what the differences are in your solutions?

For a fixed-sized stack implemented using an array, you'd need to track the size of the stack then respond appropriately to attempts to either insert another element when the stack is full or delete an element when the stack is empty. Remember, a stack is a Last-In, First-Out structure (LIFO) so the size would indicate the index of the top of the stack.

For a fixed-sized stack implemented using a List, you'd still have to do the same thing as with an array, except you'd invoke methods of List to control insertion and deletion.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!