Results 1 to 4 of 4
  1. #1
    Bilal1 is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Question How to add and remove from arraylist through different class

    Hello guys, i am new to this forum and i hope i can get some help please.

    Right now i am working on some java using BlueJ and trying to insert and remove data from an arraylist through a public class or from a new class.
    I want it to have it as like a database were you can add and remove data but have a limit of storing.


    I have tried many lines of codes but because of my low knowledge of java i haven't being able to get this done

    I really appreciate some help please.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: How to add and remove from arraylist through different class

    What you're saying is that you want one object to talk to the other. In Java land, we call that "calling a method". So, you now have to design a method. One object A has the ArrayList, that will contain the method. The other object B calls this method on object A. Basically, you will get this conversation in object B:
    Java Code:
    MyObjectA objA = new MyObjectA();
    objA.addStringToTheArrayList( "this is my first String to add" );
    objA.addStringToTheArrayList( "this is my second String to add" );
    As you can see, class A needs to contain a method called addStringToTheArrayList which has one String parameter. All that remains is the implementation in class A. That would be to take the String and put it in the ArrayList.

    Can't make it any easier than this. (Besides typing out a full working example, but that probably is the exercise you teacher gave you ;))
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to add and remove from arraylist through different class

    It sounds like maybe what you want is a delegate method. (Some book I read said that a delegate method is technically something a little different, but whatever. This is what Eclipse calls a delegate method.)

    Say you have a class, MyClass, that has a private ArrayList field. Then you write some methods in MyClass that add and remove items from the ArrayList, possibly placing some restrictions on what can be added. These methods may or may not have exactly the same signature as the add and remove methods of ArrayList.

    A related term is composition. MyClass has some of the capabilities of an ArrayList, and it contains an ArrayList, but it isn't an ArrayList, and you probably don't want it to be. You'll often hear that you should favor composition over inheritance. Some people consider it bad form to extend Swing classes, for example, preferring to resuse them through composition and delegate methods.
    Get in the habit of using standard Java naming conventions!

  4. #4
    Bilal1 is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Re: How to add and remove from arraylist through different class

    Ty guys for the reply. Well I have done the adding now but I don't know how i can get the remove item done.


    this is the code now i am using.

    import java.util.ArrayList;

    public class ItemsHolder
    {
    // A list of the products.
    private ArrayList<Product> hold;

    /**

    public ItemsHolder()
    {
    hold = new ArrayList<Product>();
    }

    public void addItem(Product item)
    {
    hold.add(item);
    }
    }

Similar Threads

  1. Replies: 2
    Last Post: 10-24-2012, 12:27 AM
  2. Why can not I remove an object from an arrayList?
    By fatabass in forum New To Java
    Replies: 6
    Last Post: 11-21-2011, 02:37 PM
  3. ArrayList remove an element from the list
    By ralf in forum New To Java
    Replies: 37
    Last Post: 07-12-2011, 08:08 PM
  4. ArrayList and remove()
    By Pragz in forum New To Java
    Replies: 1
    Last Post: 04-21-2011, 02:42 AM
  5. need help with the remove method on arrayList
    By ShinTec in forum New To Java
    Replies: 5
    Last Post: 02-16-2010, 10:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •