• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Removing specific element from array, trying to remove null output

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to remove an element from an array. I am running into an issue where when I run my program and remove a certain index. I get nulls in the place where the element/index should be removed entirely.

My output is when I execute list.display() in the main method is

Tom, Bob, Richard, John

However after I execute list.remove() and run the program. I get

Tom, null, null, Richard.




 
Saloon Keeper
Posts: 4153
160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the 'remove' method, you forget to set theList to temp in the end.

As a sidenode: if you do 'remove(1)' followed by 'remove(2)', you are in fact deleting elements 1 and 3 of the original theList. But maybe that's the intention.
 
brian leung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:in the 'remove' method, you forget to set theList to temp in the end.

As a sidenode: if you do 'remove(1)' followed by 'remove(2)', you are in fact deleting elements 1 and 3 of the original theList. But maybe that's the intention.



Sorry i'm confused on what you mean why you say i'm deleting elements 1 and 3 from the original theList. Is remove(1) and remove(2) deleting the first and second element?
 
Master Rancher
Posts: 3719
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, element 1 and element 2 are not the first and second element, because element 0 is the first, and element 1 is the second.  Confusing, but that's how most arrays and lists are numbered, in Java and most other programming.

But also, what Piet was saying is that remove(1) and remove(2) will remove element 1 (the second element) and the new element 2 (new third) , which was originally element 3 (fourth).  The first remove(1) has an effect on the numbers in the list afterwards.

So if you have an array consisting of

["A", "B", "C", "D", "E"]

and you call remove(1), you get

["A", "C", "D", "E"]

and if you now call remove(2), you get

["A", "C", "E"]

The missing B and D were originally elements 1 and 3, second and fourth, in the original array.

 
brian leung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:First, element 1 and element 2 are not the first and second element, because element 0 is the first, and element 1 is the second.  Confusing, but that's how most arrays and lists are numbered, in Java and most other programming.

But also, what Piet was saying is that remove(1) and remove(2) will remove element 1 (the second element) and the new element 2 (new third) , which was originally element 3 (fourth).  The first remove(1) has an effect on the numbers in the list afterwards.

So if you have an array consisting of

["A", "B", "C", "D", "E"]

and you call remove(1), you get

["A", "C", "D", "E"]

and if you now call remove(2), you get

["A", "C", "E"]

The missing B and D were originally elements 1 and 3, second and fourth, in the original array.



Ok thank you Mike, this makes sense being able to visualize it. I'm still trying to sweat this out. So this must mean that I'm updating the original list to the new list in the wrong place?
 
Bartender
Posts: 7431
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian, please do not edit old posts once people have responded to them. It makes their responses sound stupid. I have reverted your edit. Please re-post the new code again at the end of this thread.
 
brian leung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Brian, please do not edit old posts once people have responded to them. It makes their responses sound stupid. I have reverted your edit. Please re-post the new code again at the end of this thread.



Ah sorry, didn't want to spam and re-post.
 
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

brian leung wrote:. . . Ah sorry . . .

Apology accepted. There is nothing spammy about a new post showing the differences.
 
They weren't very bright, but they were very, very big. Ad contrast:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic