Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c1c9ebc

Browse files
Add for-each loop and arraylist get method example
1 parent 50e629e commit c1c9ebc

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to get the element by
8+
* its index using the get() method
9+
*
10+
* @author Gaurav Kukade at coderolls.com
11+
*
12+
*/
13+
public class ArrayListGetExample {
14+
15+
public static void main(String[] args) {
16+
17+
List<String> list = new ArrayList<String>();
18+
19+
list.add("Monday");
20+
list.add("Tuesday");
21+
list.add("Wednesday");
22+
list.add("Thursday");
23+
list.add("Friday");
24+
list.add("Saturday");
25+
list.add("Sunday");
26+
27+
System.out.println("Arraylist:"+ list);
28+
29+
// get element at index 3
30+
String element = list.get(3);
31+
System.out.println("\nElement at index 3 is "+ element);
32+
33+
System.out.println("\nUsing get() method in for loop: \n");
34+
35+
// using the get(0 method in for loop
36+
for(int i=0; i<list.size(); i++) {
37+
System.out.println("The element at index "+i+" is "+ list.get(i));
38+
}
39+
}
40+
41+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
/**
4+
* A java program to traverse the array using the
5+
* for-each loop i.e. enhanced for loop
6+
*
7+
* @author Guarav Kukade at coderolls.com
8+
*
9+
*/
10+
public class ArrayForEachExample {
11+
12+
public static void main(String[] args) {
13+
14+
// create an array of marks
15+
int[] numbers = { 88, 95, 65, 76, 78, 45, 95, 96, 56};
16+
17+
// traversing the array using for-each loop
18+
for(int number: numbers) {
19+
20+
System.out.println("The number is "+ number);
21+
}
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to traverse the collection using
8+
* the for-each loop i.e. enhanced for loop
9+
*
10+
* @author Gaurav Kukade at coderolls.com
11+
*
12+
*/
13+
public class ArrayListForEachExample {
14+
15+
public static void main(String[] args) {
16+
17+
// create an empty arraylist of Integer
18+
List<Integer> numbers = new ArrayList<Integer>();
19+
20+
//add number to the list
21+
numbers.add(1);
22+
numbers.add(8);
23+
numbers.add(8);
24+
numbers.add(5);
25+
numbers.add(4);
26+
numbers.add(6);
27+
numbers.add(3);
28+
numbers.add(2);
29+
numbers.add(9);
30+
31+
//traversing the collection using for-each loop
32+
for(Integer number: numbers) {
33+
System.out.println("The number is "+ number);
34+
}
35+
}
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /