File tree Expand file tree Collapse file tree 4 files changed +132
-0
lines changed
arraylist-clear-method-in-java
arraylist-contains-method
arraylist-removerange-method
java-string/java-reverse-string-using-recursion Expand file tree Collapse file tree 4 files changed +132
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .ArrayList ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ /**
7+ * A java program to remove all the elements
8+ * of ArrayList using the clear() method.
9+ *
10+ * @author Gaurav Kukade at coderolls.com
11+ *
12+ */
13+ public class ArrayListClearMethodExample {
14+
15+ public static void main (String [] args ) {
16+
17+ // create an arraylist cities
18+ List <String > cities = new ArrayList <String >();
19+
20+ // add string objects in 'cities'
21+ cities .add ("New York City" );
22+ cities .add ("Los Angeles" );
23+ cities .add ("Mountain View" );
24+ cities .add ("Austin" );
25+ cities .add ("Atlanta" );
26+
27+ // printing the cities
28+ System .out .println ("Before invoking the clear() method" );
29+ System .out .println ("Cities: " + cities ); //print the arraylist with elements
30+ System .out .println ("cities size: " + cities .size ());
31+
32+ //invoke the clear() method on arraylist
33+ cities .clear ();
34+ System .out .println ("\n After invoking the clear() method" );
35+ System .out .println ("Cities: " + cities ); // empty arraylist
36+ System .out .println ("cities size: " + cities .size ());
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .ArrayList ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+ /**
6+ * A java program to check if the ArrayList
7+ * contains the specified element.
8+ *
9+ * @author Gaurav Kukade a coderolls.com
10+ *
11+ */
12+ public class ArrayListContainsMethodExample {
13+
14+ public static void main (String [] args ) {
15+
16+ //create an empty arraylist object 'states'
17+ List <String > states = new ArrayList <String >();
18+
19+ //add state in the arraylist
20+ states .add ("California" );
21+ states .add ("Texas" );
22+ states .add ("Florida" );
23+ states .add ("New Jersey" );
24+ states .add ("Washington" );
25+
26+ //check if states contains florida
27+ boolean isPresent1 = states .contains ("Florida" );
28+ System .out .println ("Is Florida present in the list: " + isPresent1 );
29+
30+ //check if states contains alaska
31+ boolean isPresent2 =states .contains ("Alaska" );
32+ System .out .println ("\n Is Alaska present in the list: " + isPresent2 );
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .ArrayList ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ /**
7+ * A java program to remove the range of elements from
8+ * the MyArraylist (a class which extends the ArrayList)
9+ * using the removeRange() method.
10+ *
11+ * The removeRange() method is protected and is accessed in class,
12+ * subclasses and in a package, but not public.
13+ *
14+ * @author Gaurav Kukade at coderolls.com
15+ *
16+ */
17+ public class MyArrayList extends ArrayList <String > {
18+
19+ public static void main (String [] args ) {
20+
21+ MyArrayList myArrayList = new MyArrayList ();
22+
23+ myArrayList .add ("Monday" );
24+ myArrayList .add ("Tuesday" );
25+ myArrayList .add ("Wednesday" );
26+ myArrayList .add ("Thursday" );
27+ myArrayList .add ("Friday" );
28+ myArrayList .add ("Saturday" );
29+ myArrayList .add ("Sunday" );
30+
31+ System .out .println ("Arraylist before removing range of elments:\n " + myArrayList );
32+
33+ myArrayList .removeRange (2 , 4 );
34+
35+ System .out .println ("\n Arraylist after removing range of elments:\n " + myArrayList );
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package ArrayListAddExample ;
2+ /**
3+ * A java program to reverse a striong using recursion.
4+ *
5+ * @author gaurav
6+ *
7+ */
8+ public class ReverseStringUsingRecursion {
9+
10+ public static void main (String [] args ) {
11+
12+ String str = "Hello" ;
13+ System .out .println (reverse (str ));
14+
15+ }
16+
17+ public static String reverse (String str ) {
18+ if ((null == str ) || (str .length () <= 1 )) {
19+ return str ;
20+ }
21+ return reverse (str .substring (1 )) + str .charAt (0 );
22+ }
23+ }
You can’t perform that action at this time.
0 commit comments