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 13 of 13

Thread: Removing an object from an ArrayList

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Removing an object from an ArrayList

    How can I remove an object from an ArrayList? If you run my code, my delete method in the Team class does not seem to be able to remove the object that I typed from the keyboard.

    the remove() method seems so have no effect even though that is how you remove an object from arraylist object that I removed is still there

    delete method I created to remove object from ArrayList:

    HTML Code:
    	public void delete() throws IOException {
    		System.out.println("Enter the name of the file that you wish to delete information from: ");
    		String name = scan.next();
    		File w = new File(name);
    		if(w.exists())
    		{
    		    ArrayList<Player> x = new ArrayList<Player>();
    		    try {
    		       Stream<String> lines = Files.lines(Paths.get(name));
    		       lines.forEach((String t) -> {
    		          String[] parse = t.split(" ");
    		          if(parse.length<5) return;
    		          x.add(new Player(parse[0], parse[1], parse[2], parse[3], parse[4]));
    		       });
    		    } catch (IOException ex) {
    		       System.out.println("Unable to open student file." + ex.toString());
    		    }
    			System.out.println("Size of Player ArrayList: " + x.size());
    			System.out.println();
    
    			for(int i=0;i<x.size();i++){  //Prints objects from chosen file to console
    		    	System.out.println(x.get(i));
    		    }
    
    		    System.out.println("How many people would you like to remove? ");
    			int num_people = scan.nextInt();
    			System.out.println("Enter first, last, goals, caps, and assists:");
    			for(int i=0; i<num_people; i++){
    				String fstname2 = scan.next();
    				String lstname2 = scan.next();
    				String goals = scan.next();
    				String caps = scan.next();
    				String assists = scan.next();
    				x.remove(new Player(fstname2,lstname2,goals,caps,assists));
    			}
    			System.out.println("Done.");
    			System.out.println();
    			for(int i=0;i<x.size();i++){ 
    		    	System.out.println(x.get(i));
    		    }
    			BufferedWriter q = new BufferedWriter(new FileWriter(w));
    		    for(Player p : x){
    		    	q.write(p + "\n");
    		    	q.newLine();
    		    }
    		    q.close();
    		}
    		else{
    			System.out.println("File does not exist.");
            }
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Removing an object from an ArrayList

    How can I remove an object from an ArrayList?
    Look at the API doc for the ArrayList class. It has a methods to do that.
    http://docs.oracle.com/javase/8/docs/api/index.html

    The code should test the boolean value returned by the method to see if it was successful.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Removing an object from an ArrayList

    I'm already using the correct method to delete the certain object though. I don't need a boolean because I already know that it doesn't work

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Removing an object from an ArrayList

    I already know that it doesn't work
    If you know it doesn't work, why are you using it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Removing an object from an ArrayList

    Because there is no other way that I know of to delete an object. Is there an alternative to remove()?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Removing an object from an ArrayList

    Yes, the remove method is used for removing objects that are in the ArrayList from the ArrayList.
    The code needs to use a reference to an object that is in the ArrayList, not create a new object which will not be in the list.
    Unless the equals() method says the objects are the same.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Removing an object from an ArrayList

    And how can I reference an object that is already in the ArrayList?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Removing an object from an ArrayList

    And how can I reference an object that is already in the ArrayList?
    The get method returns a reference to an object in the list.

    Does the Player class have an equals() method?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Removing an object from an ArrayList

    How does the get method return a reference to an object in the list?

    I created an equals method in the Player class but have not used it.

    --- Update ---

    This is my method when I added the equals function and used a getter to get all of the objects in the existing arraylist. I made a loop to test if the object I entered in from the keyboard is equal to any of the ones in the arraylist, but it still doesn't work.


    HTML Code:
    	public void delete() throws IOException {
    		System.out.println("Enter the name of the file that you wish to delete information from: ");
    		String name = scan.next();
    		File w = new File(name);
    		if(w.exists())
    		{
    		    ArrayList<Player> x = new ArrayList<Player>();
    		    try {
    		       Stream<String> lines = Files.lines(Paths.get(name));
    		       lines.forEach((String t) -> {
    		          String[] parse = t.split(" ");
    		          if(parse.length<5) return;
    		          x.add(new Player(parse[0], parse[1], parse[2], parse[3], parse[4]));
    		       });
    		    } catch (IOException ex) {
    		       System.out.println("Unable to open student file." + ex.toString());
    		    }
    			System.out.println("Size of Player ArrayList: " + x.size());
    			System.out.println();
    
    			for(int i=0;i<x.size();i++){
    		    	System.out.println(x.get(i));
    		    }
    
    		    System.out.println("How many people would you like to remove? ");
    			int num_people = scan.nextInt();
    			System.out.println("Enter first, last, goals, caps, and assists:");
    			for(int i=0; i<num_people; i++){
    				String fstname2 = scan.next();
    				String lstname2 = scan.next();
    				String goals = scan.next();
    				String caps = scan.next();
    				String assists = scan.next();
    				Player y = new Player(fstname2,lstname2,goals,caps,assists);
    				for(int j=0;j<x.size();j++){
    					if(y.equals(x.get(j))){
    						x.remove(j);
    					}
    			    }
    			}
    			
    			System.out.println("Done.");
    			System.out.println();
    			for(int i=0;i<x.size();i++){
    		    	System.out.println(x.get(i));
    		    }
    			BufferedWriter q = new BufferedWriter(new FileWriter(w));
    		    for(Player p : x){
    		    	q.write(p + "\n");
    		    	q.newLine();
    		    }
    		    q.close();
    		}
    		else{
    			System.out.println("File does not exist.");
            }
    	}

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Removing an object from an ArrayList

    I created an equals method in the Player class but have not used it.
    What about this statement:
       if(y.equals(x.get(j))){


    it still doesn't work.
    What results does the equals method return? Try debugging the equals method. One way is to add some print statements that show what is a happening in the method and what value it is returning.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Markusovich_ (July 26th, 2019)

  12. #11
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Removing an object from an ArrayList

    I think I got it now, thanks!

    I created my equals method like this:

    HTML Code:
    	public boolean equals(Player other){
    		return(true);
    	}
    Now it works perfectly fine.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Removing an object from an ArrayList

    Now it works perfectly fine.
    Looks like that equals will match ALL the objects in the list or anywhere else.
    Have you tested it with different values?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Removing an object from an ArrayList

    I replaced

    HTML Code:
    public boolean equals(Player other){
    	return(true);
    }
    with

    HTML Code:
    public boolean equals(Object other) {
        if (this == other)
            return true;
        if (other == null || getClass() != other.getClass())
            return false;
        Player player = (Player) other;
        return (fstname.equals(player.fstname) && lstname.equals(player.lstname));
        }
    and now it works.

Similar Threads

  1. Adding Object to ArrayList
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 20th, 2013, 10:02 AM
  2. Need some help with removing and finding items from an ArrayList
    By bankston13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 30th, 2012, 08:51 PM
  3. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM
  4. Arraylist removing element
    By Stn in forum Loops & Control Statements
    Replies: 6
    Last Post: January 9th, 2011, 08:14 PM
  5. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM

Tags for this Thread