Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);

for (int i = 0; i < list.size(); i++) {
	System.out.println(list.get(i));
}

for (Integer elem : list) {
	System.out.println(elem);
}
ist.forEach(new Consumer<Integer>() {
    @Override
	public void accept(Integer elem) {
        System.out.println(elem);
    }
});


What I have tried:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);

for (int i = 0; i < list.size(); i++) {
	System.out.println(list.get(i));
}

for (Integer elem : list) {
	System.out.println(elem);
}
ist.forEach(new Consumer<Integer>() {
    @Override
	public void accept(Integer elem) {
        System.out.println(elem);
    }
});
Posted
Updated 31-Aug-18 21:58pm
Comments
KarstenK 1-Sep-18 3:50am    
What is your problem? Try to read the documentation for syntax issue. ;-)
Richard MacCutchan 1-Sep-18 4:24am    
It works exactly the same in both languages. A counter or index is used to address each element. Even in the "for each" style, as the compiler converts that for you.

1 solution

Quote:
How does iterating through an arraylist work in java or C++?

There is something very wrong in the question, because 'how arrays works' is array 101, array 102.
the only explanation I can think of is that you have made your easy way of learning by skipping some lessons.
Indeed in first, skipping lessons saved you some learning time.
But later you see that some knowledge is missing, and you start to spend more time to learn that missing knowledge. Worse is that you also waste other people time.

Millions of people have learned programming, so courses are the easy way to learn it.

We can't possibly write an explanation better than what is in courses.
The question says that you are missing an extremely fundamental part of the array concept, only a talk with a teacher or tutor can spot exactly what you don't understand and give remedy.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900