Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Best way of adding and removing?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Best way of adding and removing?

    Over the summer I am trying to get my programming skills and ADT knowledge up. Right now I am doing a registrar program where I can set up different classes like a Physics class, Humanities class, etc. and add and remove students from each class. I am currently using an array of Students but as I was writing the checks to see if a student is already registered and then add them to the class, I was thinking there was probably an easier way to do it.

    Would a linked list be an easier or more effective way of doing this? If I needed to remove a student that is in the middle of the list I could point the node to the element after the one I needed to remove, delete the node I need to remove etc. and when adding I just make a new node and point the last node to the new node. Would it be a bad idea to finish the program using a small array and then update my code to use a linked list after I get everything working or would that be a waste of time?

    It just seems like bad practice to me to keep making new arrays when I need to add or remove a student.

    Thanks


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Best way of adding and removing?

    Quote Originally Posted by J-moges View Post
    If I needed to remove a student that is in the middle of the list I could point the node to the element after the one I needed to remove, delete the node I need to remove etc.
    As long as after this....

    Quote Originally Posted by J-moges View Post
    and when adding I just make a new node and point the last node to the new node.
    and after this, you finish connecting the other node in both cases. When you break the "chain" to delete or add a "link" there are two open ends of the chain that must connect to each other or to the inserted node (unless the insert is an append)

    Quote Originally Posted by J-moges View Post
    Would it be a bad idea to finish the program using a small array and then update my code to use a linked list after I get everything working or would that be a waste of time?
    Would it be a bad idea to paint the second half of your car the wrong color, or just stop now and paint the whole car one color one time? This is something you try to determine before you write code, during the planning. Many people skip the planning (not saying that you did or did not) but for all readers, this is one example to consider when planning.

    Quote Originally Posted by J-moges View Post
    It just seems like bad practice to me to keep making new arrays when I need to add or remove a student.
    I agree. All that reading and writing involved. I like the idea of pointers too.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Best way of adding and removing?

    It just seems like bad practice to me to keep making new arrays when I need to add or remove a student.
    Look into how vectors (a.k.a. ArrayList) track "dynamic arrays". They don't always create a new array storage area every time the size of the list is modified.

    Would it be a bad idea to finish the program using a small array and then update my code to use a linked list after I get everything working or would that be a waste of time?
    This is a great application area for abstraction. Why should your front-end care about how the students are stored at all? Create an interface with the necessary features you need to implement the front-end, then you are free to write any back-end scheme you want which implements that front-end. This allows you to test and compare your code with a simple implementation you're familiar with, then work on more advanced implementations if necessary.



    I would suggest looking into set and map data structures as these will likely perform much better than list data structures in this scenario (see if you can't figure out why).

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Best way of adding and removing?

    Quote Originally Posted by jps View Post
    and after this, you finish connecting the other node in both cases. When you break the "chain" to delete or add a "link" there are two open ends of the chain that must connect to each other or to the inserted node (unless the insert is an append)

    Would it be a bad idea to paint the second half of your car the wrong color, or just stop now and paint the whole car one color one time? This is something you try to determine before you write code, during the planning. Many people skip the planning (not saying that you did or did not) but for all readers, this is one example to consider when planning.

    I agree. All that reading and writing involved. I like the idea of pointers too.
    Yeah, I wasn't explaining the whole process in the post but I understand I need to make sure both ends are pointing to the right places. I am a first year CS student and finished a intro to data structures class last semester. This project was in the first chapter (I'm just going through the book again over the summer to reinforce topics and practice) and was basically step by step and originally wanted me to use arrays (linked lists and different data types aren't covered for a few more chapters). As I was getting farther into the program it hit me that I might be able to use some of the ADTs that I learned. So I didn't really plan it out, just followed the book. I'll start looking farther ahead.


    Quote Originally Posted by helloworld922 View Post
    Look into how vectors (a.k.a. ArrayList) track "dynamic arrays". They don't always create a new array storage area every time the size of the list is modified.


    I would suggest looking into set and map data structures as these will likely perform much better than list data structures in this scenario (see if you can't figure out why).
    I'll look into the map a little more, we didn't cover it in the intro class but it is looking like a nice alternative. Thanks for the suggestions guys!

Similar Threads

  1. Help with removing objects
    By Seacats in forum Object Oriented Programming
    Replies: 4
    Last Post: May 2nd, 2013, 08:32 AM
  2. I need help with removing arrays or hiding them.
    By seaofFire in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2012, 07:09 PM
  3. [SOLVED] I'm having problems with removing arrays
    By seaofFire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 23rd, 2012, 11:18 AM
  4. adding or removing panels to panels
    By luisp88 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 04:37 PM
  5. Removing the middle cell
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 7th, 2010, 12:35 PM

Tags for this Thread