The way of Java
ArbolOne
arbolone at gmail.com
Mon Oct 28 06:48:32 UTC 2024
Thank you, you have been most kind and no more questions regarding
Learning Java.
I just needed another engineer to have a look and confirm what I had
been telling my colleagues.
Again, thanks so much.
On 2024-10-27 11:26 p.m., David Holmes wrote:
>
> Please note these lists are for discussion of OpenJDK development, not
> for getting help on using Java.
>
> Your code below seems okay so you need to check how you are setting
> the id's in the first place as the problem may be at that end.
>
> David
>
>
> On 26/10/2024 10:27 pm, ArbolOne wrote:
>>
>> Thanks!
>>
>> I realized the mistake after I posted the question, however, my real
>> concern is about displaying the data inside the ArrayList.
>>
>> // updated example
>>
>> public class NamesRecord{
>> String id;
>> ...
>> // Setters and Getters
>> public String getId(){ return this.id; }
>> }
>>
>> // Tres Dorritos Despues
>> // aoe.getAllNames() returns an ArrayList<NamesRecord> object
>> containing 13 elements
>> // each element has a unique value
>> var localArrayList = new ArrayList<NamesRecord>(aoe.getAllNames());
>> for(int i = 0; i != localArrayList.size(); i++){
>> */System.err.println(localArrayList.get(i).getId() );/* // <-- keeps
>> displaying the same value
>>
>> }
>>
>> Where did I go wrong here?
>>
>>
>> Thanks so much in advance.
>>
>>
>> On 2024-10-26 7:43 a.m., Olexandr Rotan wrote:
>>>
>>> Hello. In both examples you redeclare variable inside loop on each
>>> new iteration. This behaviour is shared for all c-like languages.
>>> Each time you enter a loop body, you "redeclare" (doubt it's JLS
>>> concept) variable and initialize it with value 1. In the end of loop
>>> you in fact increment variable, but it has no effect since variable
>>> is reinitialized on next loop iteration.
>>> Your best choice is to move variable intiialization right before the
>>> loop. This way you will get the desired behaviour.
>>> PS: just a suggestion for you as newbie. Take a look at "var"
>>> keyword. This will make your programing experience much more
>>> pleasant imo :)
>>>
>>>
>>> On Sat, Oct 26, 2024, 13:18 ArbolOne <arbolone at gmail.com> wrote:
>>>
>>> openjdk 17.0.13 2024-10-15
>>> OpenJDK Runtime Environment (build 17.0.13+11-Debian-2deb12u1)
>>> OpenJDK 64-Bit Server VM (build 17.0.13+11-Debian-2deb12u1,
>>> mixed mode, sharing)
>>>
>>> public class NamesRecord{
>>> String id;
>>> ...
>>> // Setters and Getters
>>> public String getId(){
>>> return this.id <http://this.id>;
>>> }
>>> }
>>>
>>> // Tres Dorritos Despues
>>>
>>> main(){
>>> // aoe.getAllNames() returns an ArrayList<NamesRecord>
>>> object containing 13 elements
>>> // each element has a unique value
>>> var localArrayList = new
>>> ArrayList<NamesRecord>(aoe.getAllNames());
>>> // Display all id values in NamesRecord <=== Only prints one
>>> id NamesRecord::id value 13 times
>>> for(NamesRecord mydto : localArrayList){
>>> Integer pos = 1;
>>> System.out.print(pos + " ID is : ");
>>> System.err.print(mydto.getId() );
>>> pos++; // <== This value does not increment
>>> }
>>>
>>> // Second try, but same result
>>> for(int i = 10; i != localArrayList.size(); i++){
>>> System.out.print("\'i\' value is : ");
>>> System.err.println(i); // <== This value does increment
>>> Integer pos = 1;
>>> System.out.print(pos + " ID is : ");
>>> System.err.println(localArrayList.get(i).getId() );
>>> pos++; // <== This value does not get incremented
>>> }
>>>
>>> // Third try, but the similar result
>>> // Please note that the value of pos is never incremented <==
>>> NamesRecord[] array = new NamesRecord[localArrayList.size()];
>>> array = localArrayList.toArray(array);
>>> for(NamesRecord mydto : array){
>>> Integer pos = 1;
>>> System.out.print(pos + " ID is : ");
>>> p.setTimer(1);
>>> System.err.println(mydto.getId() );
>>> pos++; // <== This value does not get incremented
>>> }
>>> }
>>>
>>> Hello.
>>> When looping through the ArrayList or even the Array[] to
>>> display the content stored in NamesRecord::id only one value is
>>> displayed; what I'd like to do is to display all the id values
>>> stored in the ArrayList or Array[] object.
>>> However, as documented in the snip above, Java behaviour is not
>>> what I expected; mind you, I am fairly new in Java.
>>> So, my question is, what am I doing wrong?
>>> If there is something wrong, would you point it out and help me
>>> resolve it, please.
>>>
>>> Is it a bug that came in the recent update I got from Debian?
>>> If so, how do I report the bug to OpenJDK?
>>>
>>> Thanks in advance.
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20241028/d9bb5048/attachment-0001.htm>
More information about the jdk-dev
mailing list