• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList error  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting this logical error whenever I run this aspect of my codes.



This is the output given


Person 0 = A C B
Person 1 = D F E

PRINT ALL PERSONS IN ALL

[D F E, D F E]

As you can see, A C B is not in the all arrayList, got replaced with DFE.
Please what am I doing wrong?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That code is confused. You have lots of casts which you should be able to avoid. You also have the wrong format for a for loop. Always use this format for iterating an array with a for loop:-
for (int i = 0; i < myArray.length; i++) ...
though you may need to modify that idiom sometimes, and a for‑each loop is often better. In fact I am surprised you can get that loop to run without an Exception. I am afraid I cannot understand how you are adding things to that List. You appear to have a List of Lists and then you are casting its elements and passing them to a static method of the Person class. That technique looks so complicated you can almost rely on its going wrong.

I am afraid I think you will have to start from scratch. Start by writing down what you actually want to do, in tiny words so simple that even I can understand it. Only then will you have any idea what sort of code to write.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many fields have you got in the Person class which are marked static?
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ab,
As Campbell told his first post dont use raw type ,go for generics where possible and it will prevent run time exception .
I am trying to understand your code and here what I have done :



Can you explain why you are doing like this :




Satya
 
Adeleke Solesty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell
All my fields are marked static, is that alright?

Thanks Majumder.
I wanna store each person that has been convert to a Person class
persons[i] = new Person(contact.get(i));

into a an array, to see if the error have to deal with ArrayList.

Thanks Campbell and Majumder, the code is working fine now, I remove the static fields and replace them with non-static ones.
Thank you very much. would keep you guys updated of any difficulty.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marking anything static without a good explanation is a serious mistake until proven otherwise. So, no it is not all right to have all your fields static.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adeleke Solesty wrote: . . . I remove the static fields and replace them with non-static ones. . . .
See, if you set the static field to "A C B" and all instances of the class see "A C B". Then you change the static field to "D F E" and all instances of the class see "D F E".
When you change to instance fields (not static), one instance has "A C B" and the other instance has "D F E". Problem solved.

The default in object‑oriented programming is for class members not to be static. There may be good reasons for things to be static, but that is the exception.
 
Adeleke Solesty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, I think one needs to refresh he's brain OOP. Bunch of thanks.
 
Adeleke Solesty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, I think one needs to refresh he's brain OOP. Bunch of thanks.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!