diff --git a/README.md b/README.md index 77eb236..1079472 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# Visit coderolls.com +# Visit [coderolls.com](https://coderolls.com) diff --git a/collections/arraylist/README.md b/collections/arraylist/README.md new file mode 100644 index 0000000..ec09ddd --- /dev/null +++ b/collections/arraylist/README.md @@ -0,0 +1,14 @@ +# Java Collections - ArrayList in Java + +This module contains articles related to ArrayList in Java. + +## Related articles + +- [ArrayList In Java](https://coderolls.com/arraylist-in-java/) +- [How To Add An Element To ArrayList In Java?](https://coderolls.com/add-element-in-arraylist/) +- [How To Change An Element In ArrayList?](https://coderolls.com/change-element-in-arraylist/) +- [How To Remove An Element From An ArrayList?](https://coderolls.com/remove-element-from-arraylist/) +- [Iterating the ArrayList In Java](https://coderolls.com/iterating-the-arraylist-in-java/) + + +Visit [coderolls](https://coderolls.com/) to read more tutorials. diff --git a/collections/arraylist/add-element-in-arraylist/ArrayListAddExample.java b/collections/arraylist/add-element-in-arraylist/ArrayListAddExample.java new file mode 100644 index 0000000..35fe325 --- /dev/null +++ b/collections/arraylist/add-element-in-arraylist/ArrayListAddExample.java @@ -0,0 +1,33 @@ +package ArrayListAddExample; + +import java.util.ArrayList; + +/** + * A Java program to add an element at the specific index. + * + * The add(int index, E element) method will add + * an element at the specified index in ArrayList. + * + * @author Guarav Kukade at coderolls.com + * + */ +public class ArrayListAddExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList using normal add method + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + + System.out.println(arrayList);// [Gaurav, Shyam] + + // adding an element to the arrayList at index 1 + arrayList.add(1, "Saurav"); + + System.out.println(arrayList); // [Gaurav, Saurav, Shyam] + + } + +} diff --git a/collections/arraylist/add-element-in-arraylist/ArrayListExample.java b/collections/arraylist/add-element-in-arraylist/ArrayListExample.java new file mode 100644 index 0000000..4dbdbcf --- /dev/null +++ b/collections/arraylist/add-element-in-arraylist/ArrayListExample.java @@ -0,0 +1,34 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A Java program to add an element to the list. + * + * The add(E e) method will add an element to + * the end of the arrayList. + * + * @author Guarav Kukade at coderolls.com + * + */ +public class ArrayListExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + + System.out.println(arrayList);// [Gaurav, Shyam] + + // adding an element to the arrayList + // element will be added to the end of the arrayList + arrayList.add("Saurav"); + + System.out.println(arrayList); // [Gaurav, Shyam, Saurav] + + } + +} diff --git a/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample.java b/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample.java new file mode 100644 index 0000000..731829d --- /dev/null +++ b/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample.java @@ -0,0 +1,41 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A Java program to add all elements of the specified collection + * to the end of the ArrayList + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListAddAllExample { + + public static void main(String[] args) { + + // ArrayList contains actresses + List actresses1 = new ArrayList(); + + actresses1.add("Natalie Portman"); + actresses1.add("Abigail Anderson"); + actresses1.add("Jennifer Lawrence"); + + System.out.println("Actresses collection 1: \n"+ actresses1 ); + + // ArrayList contains actresses + List actresses2 = new ArrayList(); + + + + actresses2.add("Angelina Jolie"); + actresses2.add("Scarlett Johansson"); + + System.out.println("Actresses collection 2: \n"+ actresses2); + + // Add actresses2 collections all elements to actresses1 collection using addAll()method + actresses1.addAll(actresses2); + + System.out.println("Actresses : \n"+ actresses1); + } +} diff --git a/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample2.java b/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample2.java new file mode 100644 index 0000000..d32e64e --- /dev/null +++ b/collections/arraylist/arraylist-addall-method-in-java/ArrayListAddAllExample2.java @@ -0,0 +1,43 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A Java program to add all elements of the specified collection + * to the ArrayList from a particular index + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListAddAllExample2 { + + public static void main(String[] args) { + + // ArrayList containing entrepreneurs + List entrepreneurs1 = new ArrayList(); + + entrepreneurs1.add("Bill Gates"); + entrepreneurs1.add("Steve Jobs"); + entrepreneurs1.add("Jeff Bezos"); + entrepreneurs1.add("Richard Branson"); + entrepreneurs1.add("Mark Cuban"); + + System.out.println("Entrepreneurs collection 1: \n"+ entrepreneurs1 ); + + // ArrayList containing entrepreneurs + List entrepreneurs2 = new ArrayList(); + + entrepreneurs2.add("Mark Zuckerberg"); + entrepreneurs2.add("Larry Page"); + entrepreneurs2.add("Larry Ellison"); + + System.out.println("Entrepreneurs collection 2: \n"+ entrepreneurs2); + + // Add entrepreneurs2 collections all elements to entrepreneurs1 collection + // starting from index 2 + entrepreneurs1.addAll(2, entrepreneurs2); + + System.out.println("Entrepreneurs : \n"+ entrepreneurs1); + } +} diff --git a/collections/arraylist/arraylist-clear-method-in-java/ArrayListClearMethodExample.java b/collections/arraylist/arraylist-clear-method-in-java/ArrayListClearMethodExample.java new file mode 100644 index 0000000..46b70b0 --- /dev/null +++ b/collections/arraylist/arraylist-clear-method-in-java/ArrayListClearMethodExample.java @@ -0,0 +1,38 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to remove all the elements + * of ArrayList using the clear() method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListClearMethodExample { + + public static void main(String[] args) { + + // create an arraylist cities + List cities = new ArrayList(); + + // add string objects in 'cities' + cities.add("New York City"); + cities.add("Los Angeles"); + cities.add("Mountain View"); + cities.add("Austin"); + cities.add("Atlanta"); + + // printing the cities + System.out.println("Before invoking the clear() method"); + System.out.println("Cities: "+ cities); //print the arraylist with elements + System.out.println("cities size: "+ cities.size()); + + //invoke the clear() method on arraylist + cities.clear(); + System.out.println("\nAfter invoking the clear() method"); + System.out.println("Cities: "+ cities); // empty arraylist + System.out.println("cities size: "+ cities.size()); + } +} diff --git a/collections/arraylist/arraylist-contains-method/ArrayListContainsMethodExample.java b/collections/arraylist/arraylist-contains-method/ArrayListContainsMethodExample.java new file mode 100644 index 0000000..a89aecf --- /dev/null +++ b/collections/arraylist/arraylist-contains-method/ArrayListContainsMethodExample.java @@ -0,0 +1,34 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +/** + * A java program to check if the ArrayList + * contains the specified element. + * + * @author Gaurav Kukade a coderolls.com + * + */ +public class ArrayListContainsMethodExample { + + public static void main(String[] args) { + + //create an empty arraylist object 'states' + List states = new ArrayList(); + + //add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + + //check if states contains florida + boolean isPresent1 = states.contains("Florida"); + System.out.println("Is Florida present in the list: "+ isPresent1); + + //check if states contains alaska + boolean isPresent2 =states.contains("Alaska"); + System.out.println("\nIs Alaska present in the list: "+ isPresent2); + } +} diff --git a/collections/arraylist/arraylist-ensurecapacity-method/ArrayListEnsureCapacityExample.java b/collections/arraylist/arraylist-ensurecapacity-method/ArrayListEnsureCapacityExample.java new file mode 100644 index 0000000..e23fd9e --- /dev/null +++ b/collections/arraylist/arraylist-ensurecapacity-method/ArrayListEnsureCapacityExample.java @@ -0,0 +1,39 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +/** + * An example java program for ensureCapacity(int MinCapacity) method + * of the ArrayList class in java. + * @author Gaurav at coderolls.com + * + */ +public class ArrayListEnsureCapacityExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + ArrayList states = new ArrayList<>(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + states.add("Minnesota"); + states.add("Colorado"); + states.add("Missouri"); + states.add("Nevada"); + System.out.println("ArrayList Elements are: \n"+ states); + //We need to add more states, so we will ensure + //that arraylist should hold 50 elements + states.ensureCapacity(50); + + System.out.println("\nWe ensured that the arraylist should hold 50 elements."); + } + +} diff --git a/collections/arraylist/arraylist-get-method/ArrayListGetExample.java b/collections/arraylist/arraylist-get-method/ArrayListGetExample.java new file mode 100644 index 0000000..b75cd5b --- /dev/null +++ b/collections/arraylist/arraylist-get-method/ArrayListGetExample.java @@ -0,0 +1,41 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to get the element by + * its index using the get() method + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListGetExample { + + public static void main(String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Arraylist:"+ list); + + // get element at index 3 + String element = list.get(3); + System.out.println("\nElement at index 3 is "+ element); + + System.out.println("\nUsing get() method in for loop: \n"); + + // using the get(0 method in for loop + for(int i=0; i arrayList = new ArrayList(); + + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + arrayList.add("Saurav"); + arrayList.add("Samiksha"); + arrayList.add("Rina"); + arrayList.add("Rina"); + + System.out.println(arrayList); + + } +} diff --git a/collections/arraylist/arraylist-in-java/ArrayListExample.java b/collections/arraylist/arraylist-in-java/ArrayListExample.java new file mode 100644 index 0000000..8c4b065 --- /dev/null +++ b/collections/arraylist/arraylist-in-java/ArrayListExample.java @@ -0,0 +1,26 @@ +package com.gaurav.ExProject; + +import java.util.ArrayList; + +/** + * A Java program showing arraylist maintains the insertion order. + * + * @author gaurav + * + */ +public class ArrayListExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + arrayList.add("Saurav"); + arrayList.add("Samiksha"); + arrayList.add("Rina"); + + System.out.println(arrayList); + } + +} diff --git a/collections/arraylist/arraylist-indexOf-method/ArrayListIndexOfExample.java b/collections/arraylist/arraylist-indexOf-method/ArrayListIndexOfExample.java new file mode 100644 index 0000000..43b8260 --- /dev/null +++ b/collections/arraylist/arraylist-indexOf-method/ArrayListIndexOfExample.java @@ -0,0 +1,38 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the indexOf() method + * of the arrayList class in java. + * + * @author Gaurav Kukade + */ +public class ArrayListIndexOfExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Florida"); + + int index = states.indexOf("Florida"); + + //prints index of the first occurrences of Florida i.e. 2 + System.out.println("The index of the Florida: "+ index); + + //trying get the index of element, which is not present + int index2 = states.indexOf("Alaska"); + + System.out.println("The index of the Alaska: " + index2); + } +} diff --git a/collections/arraylist/arraylist-isempty-method/ArrayListIsEmptyExample.java b/collections/arraylist/arraylist-isempty-method/ArrayListIsEmptyExample.java new file mode 100644 index 0000000..886e31e --- /dev/null +++ b/collections/arraylist/arraylist-isempty-method/ArrayListIsEmptyExample.java @@ -0,0 +1,44 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +/** + * An example java program for an isEmpty method + * of the ArrayList class in java. + * + * @author Gaurav Kukade at coderolls + */ +public class ArrayListIsEmptyExample { + + public static void main(String[] args) { + + // create an arraylyst teams + ArrayList teams = new ArrayList(); + + // add elements to the arraylist teams + teams.add("LA Galaxy"); + teams.add("Orlando City FC"); + teams.add("Portland Timbers"); + teams.add("Columbus Crew"); + + boolean isTeamsEmpty = teams.isEmpty(); + + if(isTeamsEmpty) { + System.out.println("Teams arraylist is empty."); + } + else { + System.out.println("Teams arraylist is not empty."); + } + + // create an arraylist players + ArrayList players = new ArrayList(); + + boolean isPlayersEmpty = players.isEmpty(); + + if(isPlayersEmpty) { + System.out.println("Players arraylist is empty."); + } + else { + System.out.println("Players arraylist is not empty."); + } + } +} diff --git a/collections/arraylist/arraylist-lastindexof-method/ArrayListLastIndexOfExample.java b/collections/arraylist/arraylist-lastindexof-method/ArrayListLastIndexOfExample.java new file mode 100644 index 0000000..52ff0ab --- /dev/null +++ b/collections/arraylist/arraylist-lastindexof-method/ArrayListLastIndexOfExample.java @@ -0,0 +1,39 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the indexOf() method + * of the arrayList class in java. + * + * @author Gaurav Kukade + */ +public class ArrayListLastIndexOfExample { + +public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Florida"); + + int index = states.lastIndexOf("Florida"); + + //prints index of the last occurrences of Florida i.e. 6 + System.out.println("The last index of the Florida: "+ index); + + //trying get the index of element, which is not present + int index2 = states.lastIndexOf("Alaska"); + + System.out.println("The index of the Alaska: " + index2); + } + +} \ No newline at end of file diff --git a/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample.java b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample.java new file mode 100644 index 0000000..d25c5b1 --- /dev/null +++ b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample.java @@ -0,0 +1,35 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.ListIterator; +/** + * An example java program about the listIterator() + * method of the arrayList. + * + * @author coderolls.com + */ +public class ArrayListListIteratorExample { + + public static void main(String[] args) { + + ArrayList states = new ArrayList<>(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + ListIterator itr = states.listIterator(); + + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} diff --git a/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample2.java b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample2.java new file mode 100644 index 0000000..3f9c41e --- /dev/null +++ b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorExample2.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.ListIterator; +/** + * An example java program about the listIterator() + * method of the arrayList. + * + * @author coderolls.com + */ +public class ArrayListListIteratorExample2 { + + public static void main(String[] args) { + + ArrayList states = new ArrayList<>(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + //given index 3 iterator start from element with index 3 + ListIterator itr = states.listIterator(3); + + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} diff --git a/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorException.java b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorException.java new file mode 100644 index 0000000..a62a1cb --- /dev/null +++ b/collections/arraylist/arraylist-listiterator-method/ArrayListListIteratorException.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.ListIterator; +/** + * An example java program about the listIterator() + * method of the arrayList. + * + * @author coderolls.com + */ +public class ArrayListListIteratorException { + + public static void main(String[] args) { + + ArrayList states = new ArrayList<>(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + //given index 120, it will throw IndexOutOfBoundsException + ListIterator itr = states.listIterator(120); + + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} diff --git a/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllExample.java b/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllExample.java new file mode 100644 index 0000000..0033f0c --- /dev/null +++ b/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllExample.java @@ -0,0 +1,49 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +/** + * A java program showing an example for the removeAll() + * method of the ArrayList class in Java. + * + * @author gaurav + */ +public class ArrayListRemoveAllExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + states.add("Minnesota"); + states.add("Colorado"); + states.add("Missouri"); + states.add("Nevada"); + + // create another ArrayList for items to be removed + List statesToBeRemoved = new ArrayList(); + statesToBeRemoved.add("Minnesota"); + statesToBeRemoved.add("Missouri"); + statesToBeRemoved.add("Montana"); + statesToBeRemoved.add("Michigan"); + + System.out.println("The states list before the removeAll() method call: \n" + states); + + // removes all elements specified in the ArrayList statesToBeRemoved + states.removeAll(statesToBeRemoved); + + System.out.println("\nThe states list after the removeAll() method call: \n" + states); + } +} diff --git a/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllNullPointerException.java b/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllNullPointerException.java new file mode 100644 index 0000000..c6dbb56 --- /dev/null +++ b/collections/arraylist/arraylist-removeall-method/ArrayListRemoveAllNullPointerException.java @@ -0,0 +1,46 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +/** + * A java program showing a NullPointerException case + * for the removeAll() method of the ArrayList class in Java. + * + * @author gaurav + */ +public class ArrayListRemoveAllNullPointerException { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + states.add("Minnesota"); + states.add("Colorado"); + states.add("Missouri"); + states.add("Nevada"); + + // create another ArrayList with null value, + //so that removeAll will throw NullPointerException + List statesToBeRemoved =null; + + System.out.println("The states list before the removeAll() method call: \n" + states); + + // removes all elements specified in the ArrayList statesToBeRemoved + states.removeAll(statesToBeRemoved); + + System.out.println("\nThe states list after the removeAll() method call: \n" + states); + } +} diff --git a/collections/arraylist/arraylist-removeif-method/ArrayListRemoveIfExample.java b/collections/arraylist/arraylist-removeif-method/ArrayListRemoveIfExample.java new file mode 100644 index 0000000..27c0c29 --- /dev/null +++ b/collections/arraylist/arraylist-removeif-method/ArrayListRemoveIfExample.java @@ -0,0 +1,37 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +/** + * A java program showing an example for the removeIf() + * method of the ArrayList class in Java. + * + * @author gaurav + */ +public class ArrayListRemoveIfExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist, Florida multiple times + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Florida"); + + System.out.println("The states list before the removeIf() call: \n" + states); + + // a predicate for the condition + Predicate p = s -> s.equals("Florida"); + states.removeIf(p); // removes all elements which satisfy the predicate p + + System.out.println("\nThe states list after the removeIf() call: \n" + states); + } +} diff --git a/collections/arraylist/arraylist-removerange-method/MyArrayList.java b/collections/arraylist/arraylist-removerange-method/MyArrayList.java new file mode 100644 index 0000000..a6bcc81 --- /dev/null +++ b/collections/arraylist/arraylist-removerange-method/MyArrayList.java @@ -0,0 +1,37 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to remove the range of elements from + * the MyArraylist (a class which extends the ArrayList) + * using the removeRange() method. + * + * The removeRange() method is protected and is accessed in class, + * subclasses and in a package, but not public. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class MyArrayList extends ArrayList { + + public static void main(String[] args) { + + MyArrayList myArrayList = new MyArrayList(); + + myArrayList.add("Monday"); + myArrayList.add("Tuesday"); + myArrayList.add("Wednesday"); + myArrayList.add("Thursday"); + myArrayList.add("Friday"); + myArrayList.add("Saturday"); + myArrayList.add("Sunday"); + + System.out.println("Arraylist before removing range of elments:\n"+ myArrayList); + + myArrayList.removeRange(2, 4); + + System.out.println("\nArraylist after removing range of elments:\n"+ myArrayList); + } +} diff --git a/collections/arraylist/arraylist-retainall-method/ArrayListRetainAllExample.java b/collections/arraylist/arraylist-retainall-method/ArrayListRetainAllExample.java new file mode 100644 index 0000000..265abbe --- /dev/null +++ b/collections/arraylist/arraylist-retainall-method/ArrayListRetainAllExample.java @@ -0,0 +1,51 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to to eplain the retainAll() method of the Arraylist class. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRetainAllExample { + + public static void main(String[] args) { + + //create an empty arraylist of Integer to retain elements + List numbers1 = new ArrayList(); + + //add number to the list + numbers1.add(4); + numbers1.add(5); + numbers1.add(6); + + + //create an empty arraylist of Integer + List numbers2 = new ArrayList(); + + // add number to the list + numbers2.add(1); + numbers2.add(2); + numbers2.add(3); + numbers2.add(4); + numbers2.add(5); + numbers2.add(6); + numbers2.add(7); + numbers2.add(8); + numbers2.add(9); + + + System.out.println("Lists before the method call"); + System.out.println("numbers1: "+ numbers1); + System.out.println("numbers2: "+ numbers2); + + //retain all the elements of numbers1 in the list numbers2 + numbers2.retainAll(numbers1); + + System.out.println("\nLists after the method call"); + System.out.println("numbers1: "+ numbers1); + System.out.println("numbers2: "+ numbers2); + } +} diff --git a/collections/arraylist/arraylist-retainall-method/RetainAllNullPointerExceptionExample.java b/collections/arraylist/arraylist-retainall-method/RetainAllNullPointerExceptionExample.java new file mode 100644 index 0000000..942c2ed --- /dev/null +++ b/collections/arraylist/arraylist-retainall-method/RetainAllNullPointerExceptionExample.java @@ -0,0 +1,39 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; +/** + * A java program to show the NullPointerException in retainAll() method + * of the arrayList class. + * + * @author gaurav + * + */ +public class RetainAllNullPointerExceptionExample { + + public static void main(String[] args) { + + //create an empty arraylist of Integer to retain elements + List numbers1 = null; + + //create an empty arraylist of Integer + List numbers2 = new ArrayList(); + + // add number to the list + numbers2.add(1); + numbers2.add(2); + numbers2.add(3); + numbers2.add(4); + + System.out.println("Lists before the method call"); + System.out.println("numbers1: "+ numbers1); + System.out.println("numbers2: "+ numbers2); + + //retain all the elements of numbers1 in the list numbers2 + numbers2.retainAll(numbers1); + + System.out.println("\nLists after the method call"); + System.out.println("numbers1: "+ numbers1); + System.out.println("numbers2: "+ numbers2); + } +} diff --git a/collections/arraylist/change-element-in-arraylist/ArrayListSetExample.java b/collections/arraylist/change-element-in-arraylist/ArrayListSetExample.java new file mode 100644 index 0000000..245ffc3 --- /dev/null +++ b/collections/arraylist/change-element-in-arraylist/ArrayListSetExample.java @@ -0,0 +1,34 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A java program to change an element at + * particular index in ArrayList. + * + * We can use 'public E set(int index, E element)' method + * to change an element. + * + * @author Guarav Kukade at coderolls.com + * + */ +public class ArrayListSetExample { + + public static void main(String[] args) { +ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList using normal add method + arrayList.add("Gaurav"); + arrayList.add("Shyam"); + arrayList.add("Pradnya"); + + System.out.println(arrayList);// [Gaurav, Shyam, Pradnya] + + // change an element at index 1 + arrayList.set(1, "Saurav"); + + System.out.println(arrayList); // [Gaurav, Saurav, Pradnya] + + } + +} diff --git a/collections/arraylist/convert-arraylist-to-array/ArrayListToArray.java b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray.java new file mode 100644 index 0000000..1e0fd8b --- /dev/null +++ b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray.java @@ -0,0 +1,39 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A java program to convert an ArrayList to array using + * toArray method. + * + * toArray() method returns Object[] + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListToArray { + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList + arrayList.add("Meta"); + arrayList.add("Apple"); + arrayList.add("Amazon"); + arrayList.add("Netflix"); + arrayList.add("Microsoft"); + arrayList.add("Google"); + + System.out.println("ArrayList: \n"+ arrayList); + + // convert ArrayList to array + Object[] objects = arrayList.toArray(); + + System.out.println("\nArray:"); + + //we will receive the object array, so iterate on it to print each element + for(Object obj:objects) { + System.out.println(obj); + } + } +} diff --git a/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java new file mode 100644 index 0000000..88b17da --- /dev/null +++ b/collections/arraylist/convert-arraylist-to-array/ArrayListToArray2.java @@ -0,0 +1,42 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A java program to convert an ArrayList to array using + * toArray method. + * + * toArray(T[] a) method returns T[] + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListToArray2 { + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList + arrayList.add("Meta"); + arrayList.add("Apple"); + arrayList.add("Amazon"); + arrayList.add("Netflix"); + arrayList.add("Microsoft"); + arrayList.add("Google"); + + System.out.println("ArrayList: \n"+ arrayList); + + // convert ArrayList to array + // pass a new String array of the arrayList size as a param to toArray + //String[] elements = arrayList.toArray(new String[arrayList.size()]); + + //kindly read the 'Important Note' below + String[] elements = arrayList.toArray(new String[0]); + + System.out.println("\nArray:"); + //we will receive the string array, so iterate on it to print each element + for(String str:elements) { + System.out.println(str); + } + } +} diff --git a/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample1.java b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample1.java new file mode 100644 index 0000000..b18f159 --- /dev/null +++ b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample1.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to iterate through arraylist + * using the basic for loop. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListIterateExample1 { + + public static void main (String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using the basic for loop.......\n"); + + for(int i =0; i list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using enhanced for loop i.e. For-each loop.......\n"); + + for(String str:list){ + //we get 'str' in for-each loop + System.out.println("Object form the ArrayList is " + str); + } + } +} \ No newline at end of file diff --git a/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample3.java b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample3.java new file mode 100644 index 0000000..9726af2 --- /dev/null +++ b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample3.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to iterate through arraylist + * using while loop. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListIterateExample3 { + + public static void main (String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using while loop.......\n"); + + int i=0; + // while loop condition will be true till i is less the the list size + while (list.size()>i) { + System.out.println("Object form the ArrayList at "+ i + " is " + list.get(i)); + i++; + } + } +} \ No newline at end of file diff --git a/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample4.java b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample4.java new file mode 100644 index 0000000..629f3ea --- /dev/null +++ b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample4.java @@ -0,0 +1,41 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * A java program to iterate through arraylist + * using Iterator interface. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListIterateExample4 { + + public static void main (String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using Iterator interface........\n"); + + // getting iterator + Iterator iterator = list.iterator(); + + // hasNext() returns true only if object is available at next call + while (iterator.hasNext()) { + + //next() returns obejct, we can cast it to reuqired type + String str = (String) iterator.next(); + System.out.println("Object form the ArrayList is " + str); + } + } +} \ No newline at end of file diff --git a/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample5.java b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample5.java new file mode 100644 index 0000000..fc7b89d --- /dev/null +++ b/collections/arraylist/iterating-the-arraylist-in-java/ArrayListIterateExample5.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to iterate through arraylist + * using forEach method and lamda expression. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListIterateExample5 { + + public static void main (String[] args) { + + List list = new ArrayList(); + + list.add("Monday"); + list.add("Tuesday"); + list.add("Wednesday"); + list.add("Thursday"); + list.add("Friday"); + list.add("Saturday"); + list.add("Sunday"); + + System.out.println("Iterating through ArrayList using forEach method and lamda expression........\n"); + + // add lambda expressionin the foreach loop + list.forEach(s -> System.out.println("Object form the ArrayList is "+ s)); + } +} \ No newline at end of file diff --git a/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample.java b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample.java new file mode 100644 index 0000000..65c87a3 --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A Java program to remove an element from ArrayList + * using the remove(Object o) method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRemoveExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList using normal add method + arrayList.add("Red"); + arrayList.add("Green"); + arrayList.add("Pink"); + + System.out.println("ArrayList before removing an element"+ arrayList); // ArrayList before removing an element[Red, Green, Pink] + + //Remove "Pink" from the arrayList + arrayList.remove("Pink"); + + System.out.println("ArrayList after removing an element"+ arrayList);// ArrayList after removing an element[Red, Green] + } +} diff --git a/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample2.java b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample2.java new file mode 100644 index 0000000..b9f098a --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveExample2.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; + +/** + * A Java program to remove an element from ArrayList + * using the remove(int index) method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRemoveExample2 { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //adding elements to the arrayList using normal add method + arrayList.add("Apple"); + arrayList.add("Orange"); + arrayList.add("Guava"); + arrayList.add("Banana"); + + System.out.println("ArrayList before removing an element"+ arrayList); // ArrayList before removing an element[Apple, Orange, Guava, Banana] + + //Remove the element at the specified index i.e. 2 + //Remove "Guava" from the arrayList + arrayList.remove(2); + + System.out.println("ArrayList after removing an element"+ arrayList);// ArrayList after removing an element[Apple, Orange, Banana] + } +} diff --git a/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample.java b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample.java new file mode 100644 index 0000000..1519655 --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample.java @@ -0,0 +1,35 @@ +package com.gaurav.ExProject2; + +import java.util.ArrayList; +/** + * A Java program to remove an object from ArrayList + * using the remove(int index) method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRemoveObjectExample { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //create Employee objects + Employee john = new Employee(100, "John Doe", 20000); + Employee nathasha = new Employee(101, "Natasha Putin", 28000); + Employee remo = new Employee(102, "Remo Smith", 40000); + + //adding employee objects to the arrayList using normal add method + arrayList.add(john); + arrayList.add(nathasha); + arrayList.add(remo); + + System.out.println("ArrayList before removing an employee Object:\n"+ arrayList); + + //Remove object using the remo0ve(Object o) method + //Remove employee nathasha from the arrayList + arrayList.remove(nathasha); + + System.out.println("\nArrayList after removing an employee Object:\n"+ arrayList); + } +} diff --git a/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample2.java b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample2.java new file mode 100644 index 0000000..7117524 --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/ArrayListRemoveObjectExample2.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject2; + +import java.util.ArrayList; + +/** + * A Java program to remove an object from ArrayList + * using the remove(int index) method. + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListRemoveObjectExample2 { + + public static void main(String[] args) { + + ArrayList arrayList = new ArrayList(); + + //create Employee objects + Employee john = new Employee(100, "John Doe", 20000); + Employee nathasha = new Employee(101, "Natasha Putin", 28000); + Employee remo = new Employee(102, "Remo Smith", 40000); + + //adding employee objects to the arrayList using normal add method + arrayList.add(john); + arrayList.add(nathasha); + arrayList.add(remo); + + System.out.println("ArrayList before removing an employee Object:\n"+ arrayList); + + //Remove object using the remove(int index) method + //Remove employee nathasha from the arrayList, so pass natasha's index + arrayList.remove(1); + + System.out.println("\nArrayList after removing an employee Object:\n"+ arrayList); + } +} diff --git a/collections/arraylist/remove-element-from-arraylist/Employee.java b/collections/arraylist/remove-element-from-arraylist/Employee.java new file mode 100644 index 0000000..0e63c8f --- /dev/null +++ b/collections/arraylist/remove-element-from-arraylist/Employee.java @@ -0,0 +1,52 @@ +package com.gaurav.ExProject2; +/** + * An employee class for ArrayList remove example + * @author Gaurav Kukade at coderolls.com + * + */ +public class Employee { + + private int id; + + private String name; + + private int salary; + + // a constructor will all the fields + public Employee(int id, String name, int salary) { + this.id = id; + this.name = name; + this.salary = salary; + } + + // getters and setters for the variables + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getSalary() { + return salary; + } + + public void setSalary(int salary) { + this.salary = salary; + } + + // Override to string method to print student objects + @Override + public String toString() { + return "{"+this.getId()+", "+ this.getName()+ ", "+this.getSalary()+"}"; + } +} diff --git a/collections/arraylist/sublist-method-in-arraylist/ArrayListSubListExample.java b/collections/arraylist/sublist-method-in-arraylist/ArrayListSubListExample.java new file mode 100644 index 0000000..8a222ed --- /dev/null +++ b/collections/arraylist/sublist-method-in-arraylist/ArrayListSubListExample.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the subList() method of the ArrayList class in Java + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListSubListExample { + + public static void main(String[] args) { + + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + + System.out.println("The states list: \n"+ states); + + // sublist the states list from 1 to 3 index + List statesSubList = states.subList(1, 3); + + System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList); + } +} diff --git a/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample1.java b/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample1.java new file mode 100644 index 0000000..7b9bbe3 --- /dev/null +++ b/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample1.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the subList() methods + * IndexOutOfBoundsException exception case. + * + * @author Gaurav Kukade at coderolls.com + */ +public class SubListMethodExceptionExample1 { + + public static void main(String[] args) { + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + + System.out.println("The states list: \n"+ states); + + // will throw IndexOutOfBoundsException + List statesSubList = states.subList(1, 7); + + System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList); + } +} diff --git a/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample2.java b/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample2.java new file mode 100644 index 0000000..b134bc6 --- /dev/null +++ b/collections/arraylist/sublist-method-in-arraylist/SubListMethodExceptionExample2.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to explain the subList() methods + * IllegalArgumentException exception case. + * + * @author Gaurav Kukade at coderolls.com + */ +public class SubListMethodExceptionExample2 { + + public static void main(String[] args) { + // create an empty arraylist object 'states' + List states = new ArrayList(); + + // add state in the arraylist + states.add("California"); + states.add("Texas"); + states.add("Florida"); + states.add("New Jersey"); + states.add("Washington"); + + System.out.println("The states list: \n"+ states); + + // will throw IllegalArgumentException + List statesSubList = states.subList(3, 1); + + System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList); + } +} diff --git a/collections/linkedlist/README.md b/collections/linkedlist/README.md new file mode 100644 index 0000000..c2945f3 --- /dev/null +++ b/collections/linkedlist/README.md @@ -0,0 +1,9 @@ +# Java Collections - LinkedList in Java + +This module contains articles related to LinkedList in Java. + +## Related articles + +- [LinkedList In Java](https://coderolls.com/linkedlist-in-java/) + +Visit [coderolls](https://coderolls.com/) to read more tutorials. diff --git a/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample.java b/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample.java new file mode 100644 index 0000000..13ce6f9 --- /dev/null +++ b/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to add an element to the LinkedList. + * + * The add(E e) method will add an element to + * the end of the linkedList. + * + * @author coderolls.com + * + */ +public class LinkedListAddExample { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + //adding elements to the linkedList + linkedList.add("John"); + linkedList.add("Peter"); + + System.out.println(linkedList);// [John, Peter] + + // adding an element to the linkedList + // element will be added to the end of the linkedList + linkedList.add("Noah"); + + System.out.println(linkedList); // [John, Peter, Noah] + } +} diff --git a/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample2.java b/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample2.java new file mode 100644 index 0000000..978faad --- /dev/null +++ b/collections/linkedlist/add-element-in-linkedlist/LinkedListAddExample2.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to add an element to the linkedlist + * at the specific index. + * + * The add(int index, E element) method will add + * an element at the specified index in LinkedList. + * + * @author coderolls.com + * + */ +public class LinkedListAddExample2 { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + //adding elements to the linkedList using normal add method + linkedList.add("John"); + linkedList.add("Peter"); + linkedList.add("Karan"); + + System.out.println(linkedList);// [John, Peter, Karan] + + // adding an element to the LinkedList at index 1 + linkedList.add(1, "Noah"); + + System.out.println(linkedList); // [John, Noah, Peter, Karan] + } +} \ No newline at end of file diff --git a/collections/linkedlist/check-if-linkedlist-contains-an-element/LinkedListContains.java b/collections/linkedlist/check-if-linkedlist-contains-an-element/LinkedListContains.java new file mode 100644 index 0000000..4fb2e0b --- /dev/null +++ b/collections/linkedlist/check-if-linkedlist-contains-an-element/LinkedListContains.java @@ -0,0 +1,40 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to check if the element is present + * in the linkedList using the contains(Object o) method. + * + * @author coderolls.com + * + */ +public class LinkedListContains { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("Uber"); + linkedList.add("Ola"); + linkedList.add("Didi"); + linkedList.add("Lyft"); + linkedList.add("Yandex"); + + System.out.println("LinkedList is as follows: \n"+ linkedList); + + + // checks if the linkedList contains "Ola" + // returns true if present + boolean isOlaPresnet = linkedList.contains("Ola"); // true + + System.out.println("\nCheck if linkedList contains Ola: "+ isOlaPresnet); + + // checks if the linkedList contains "Grab" + // returns true if present + boolean isGrabPresnet = linkedList.contains("Grab"); // false + + System.out.println("\nCheck if linkedList contains Grab: "+ isGrabPresnet); + + } +} diff --git a/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray.java b/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray.java new file mode 100644 index 0000000..e3d4365 --- /dev/null +++ b/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A java program to convert linkedlist to an array + * using the toArray() method. + * + * @author coderolls.com + * + */ +public class LinkedListToArray { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Walmart"); + linkedList.add("Amazon"); + linkedList.add("Apple"); + linkedList.add("Microsoft"); + linkedList.add("Google"); + linkedList.add("Uber"); + linkedList.add("Tesla"); + + System.out.println("Printing the linkedList: "); + System.out.println(linkedList); + + // convert linkedList to Object[] + Object[] arr = linkedList.toArray(); + + System.out.println("\nPrinting the object array elements: "); + for(Object obj:arr) { + System.out.println(obj.toString()); + } + } +} diff --git a/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray2.java b/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray2.java new file mode 100644 index 0000000..12203c4 --- /dev/null +++ b/collections/linkedlist/convert-linkedlist-to-array/LinkedListToArray2.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A java program to convert linkedlist to an array + * using the toArray(T[] a) method. + * + * @author coderolls.com + * + */ +public class LinkedListToArray2 { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Walmart"); + linkedList.add("Amazon"); + linkedList.add("Apple"); + linkedList.add("Microsoft"); + linkedList.add("Google"); + linkedList.add("Uber"); + linkedList.add("Tesla"); + + System.out.println("Printing the linkedList: "); + System.out.println(linkedList); + + // convert linkedList to String array + String[] arr = linkedList.toArray(new String[linkedList.size()]); + + System.out.println("\nPrinting the String array elements: "); + for(String str:arr) { + System.out.println(str); + } + } +} diff --git a/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetElement.java b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetElement.java new file mode 100644 index 0000000..62fe1b9 --- /dev/null +++ b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetElement.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get an element from + * the linkedlist by it's index. + * + * @author coderolls.com + */ +public class LinkedListGetElement { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Adidas"); + linkedList.add("Air"); + linkedList.add("Nike"); + linkedList.add("Puma"); + linkedList.add("Reebok"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get element at index 3 + String element = linkedList.get(3); // Puma + + System.out.println("\nElement at index 3 is: "+ element); + } +} diff --git a/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetFirst.java b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetFirst.java new file mode 100644 index 0000000..883797e --- /dev/null +++ b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetFirst.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get the first element from + * the linkedlist using getFirst() method. + * + * @author coderolls.com + */ +public class LinkedListGetFirst { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Adidas"); + linkedList.add("Air"); + linkedList.add("Nike"); + linkedList.add("Puma"); + linkedList.add("Reebok"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get first element + String element = linkedList.getFirst(); // Adidas + + System.out.println("\nFirst element of the linkedList is: "+ element); + } +} diff --git a/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetLast.java b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetLast.java new file mode 100644 index 0000000..a72f44e --- /dev/null +++ b/collections/linkedlist/get-an-element-from-linkedlist/LinkedListGetLast.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get the last element of + * the linkedlist using getLast() method. + * + * @author coderolls.com + */ +public class LinkedListGetLast { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Adidas"); + linkedList.add("Air"); + linkedList.add("Nike"); + linkedList.add("Puma"); + linkedList.add("Reebok"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get first element + String element = linkedList.getLast(); // Adidas + + System.out.println("\nLast element of the linkedList is: "+ element); + } +} diff --git a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java new file mode 100644 index 0000000..aad7aaf --- /dev/null +++ b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListIndexOf.java @@ -0,0 +1,43 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get the first index of the element from + * the linkedlist using indexOf(Object o) method. + * + * @author coderolls.com + */ +public class LinkedListIndexOf { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("PepsiCo"); + linkedList.add("DrPepper"); + linkedList.add("GoldSpot"); + linkedList.add("GoldSpot"); + linkedList.add("CocaCola"); + linkedList.add("Moxie"); + linkedList.add("GoldSpot"); + linkedList.add("Moxie"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get first index of GoldSpot + int index = linkedList.indexOf("DrPepper"); // 1 + + System.out.println("\nIndex of the DrPepper in linkedList is: "+ index); + + // get first index of GoldSpot + int indexGS = linkedList.indexOf("GoldSpot"); // 2 + + System.out.println("\nIndex of the GoldSpot in linkedList is: "+ indexGS); + + // get first index of Moxie + int indexMoxie = linkedList.indexOf("Moxie"); // 5 + + System.out.println("\nIndex of the Moxie in linkedList is: "+ indexMoxie); + } +} diff --git a/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java new file mode 100644 index 0000000..b15b3ff --- /dev/null +++ b/collections/linkedlist/get-an-index-of-element-of-linkedlist/LinkedListLastIndexOf.java @@ -0,0 +1,43 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to get the last index of the element from + * the linkedlist using lastIndexOf(Object o) method. + * + * @author coderolls.com + */ +public class LinkedListLastIndexOf { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("PepsiCo"); + linkedList.add("DrPepper"); + linkedList.add("GoldSpot"); + linkedList.add("GoldSpot"); + linkedList.add("CocaCola"); + linkedList.add("Moxie"); + linkedList.add("GoldSpot"); + linkedList.add("Moxie"); + + System.out.println("LinkedList: "); + System.out.println(linkedList); + + // get last index of GoldSpot + int index = linkedList.lastIndexOf("DrPepper"); // 1 + + System.out.println("\nLast index of the DrPepper in linkedList is: "+ index); + + // get last index of GoldSpot + int indexGS = linkedList.lastIndexOf("GoldSpot"); // 6 + + System.out.println("\nLast index of the GoldSpot in linkedList is: "+ indexGS); + + // get last index of Moxie + int indexMoxie = linkedList.lastIndexOf("Moxie"); // 7 + + System.out.println("\nLast index of the Moxie in linkedList is: "+ indexMoxie); + } +} diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForEachMethod.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForEachMethod.java new file mode 100644 index 0000000..f9889c4 --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForEachMethod.java @@ -0,0 +1,30 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate over linkedList + * using the java 8 foreach method. + * + * @author coderolls.com + */ +public class LinkedListForEachMethod { + + public static void main(String[] args) { + + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + + System.out.println("Iterating through LinkedList using forEach method and lamda expression........\n"); + + // add lambda expression in the foreach method + linkedList.forEach(s -> System.out.println("Object form the LinkedList is " + s)); + } +} diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForLoop.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForLoop.java new file mode 100644 index 0000000..92428da --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForLoop.java @@ -0,0 +1,38 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate over linkedList + * using the basic for loop. + * + * @author coderolls.com + * + */ +public class LinkedListForLoop { + + public static void main(String[] args) { + + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + + System.out.println("Iterating through LinkedList using the basic for loop!\n"); + + int len = linkedList.size(); + + // iterate over linkedList using basic for loop + for (int i = 0; i < len; i++) { + + // get String object from linkedList at index i + String str = linkedList.get(i); + System.out.println("Object form the LinkedList at " + i + " is " + str); + } + } +} diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForeachLoop.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForeachLoop.java new file mode 100644 index 0000000..cd4f7eb --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListForeachLoop.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate over linkedList + * using the foreach loop. + * + * @author coderolls.com + * + */ +public class LinkedListForeachLoop { + + public static void main(String[] args) { + + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + + System.out.println("Iterating through LinkedList using the foreach loop!\n"); + + // iterate over linkedList using foreach loop + for (String str: linkedList) { + System.out.println("Object form the LinkedList is "+str); + } + } +} diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListIterator.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListIterator.java new file mode 100644 index 0000000..163e27a --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListIterator.java @@ -0,0 +1,37 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate through LinkedList using Iterator interface. + * + * @author coderolls.com + * + */ +public class LinkedListIterator { + + public static void main(String[] args) { + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + System.out.println("Iterating through LinkedList using Iterator interface........\n"); + + // getting iterator + Iterator iterator = linkedList.iterator(); + + // hasNext() returns true only if object is available at next call + while (iterator.hasNext()) { + + // next() returns obejct, we can cast it to reuqired type + String str = iterator.next(); + System.out.println("Object form the LinkedList is " + str); + } + } +} \ No newline at end of file diff --git a/collections/linkedlist/iterate-linkedlist-in-java/LinkedListWhileLoop.java b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListWhileLoop.java new file mode 100644 index 0000000..3293863 --- /dev/null +++ b/collections/linkedlist/iterate-linkedlist-in-java/LinkedListWhileLoop.java @@ -0,0 +1,44 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.List; + +/** + * A java program to iterate over linkedList + * using the while loop. + * + * @author coderolls.com + * + */ +public class LinkedListWhileLoop { + + public static void main(String[] args) { + + List linkedList = new LinkedList(); + + linkedList.add("Mango"); + linkedList.add("Banana"); + linkedList.add("Grapes"); + linkedList.add("Watermelon"); + linkedList.add("Apple"); + linkedList.add("Avocado"); + + System.out.println("Iterating through LinkedList using the while loop!\n"); + + int len = linkedList.size(); + + // iterate over linkedList using while loop + int i =0; + + while(i linkedList = new LinkedList(); + linkedList.add("Austin"); + linkedList.add("Boston"); + linkedList.add("Atlanta"); + linkedList.add("Madison"); + linkedList.add("Columbia"); + + System.out.println(linkedList); + + // add element at the beginning + linkedList.addFirst("Albany"); + System.out.println(linkedList); + + } + +} diff --git a/collections/linkedlist/linkedlist-addlast-method/LinkedListAddLast.java b/collections/linkedlist/linkedlist-addlast-method/LinkedListAddLast.java new file mode 100644 index 0000000..30fec6f --- /dev/null +++ b/collections/linkedlist/linkedlist-addlast-method/LinkedListAddLast.java @@ -0,0 +1,24 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +public class LinkedListAddLast { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("Austin"); + linkedList.add("Boston"); + linkedList.add("Atlanta"); + linkedList.add("Madison"); + linkedList.add("Columbia"); + + System.out.println(linkedList); + + // add element at the end + linkedList.addLast("Albany"); + System.out.println(linkedList); + + } + +} diff --git a/collections/linkedlist/linkedlist-in-java/LinkedAllowDuplicates.java b/collections/linkedlist/linkedlist-in-java/LinkedAllowDuplicates.java new file mode 100644 index 0000000..03b62bb --- /dev/null +++ b/collections/linkedlist/linkedlist-in-java/LinkedAllowDuplicates.java @@ -0,0 +1,25 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +/** + * A Java program to prove that LinkedList Allows null values + * @author coderolls.com + * + */ +public class LinkedAllowDuplicates { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("John"); + linkedList.add(null); + linkedList.add("Mark"); + linkedList.add(null); + linkedList.add("Ruby"); + linkedList.add("Lucy"); + linkedList.add("Vikram"); + linkedList.add(null); + + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/linkedlist-in-java/LinkedAllowNull.java b/collections/linkedlist/linkedlist-in-java/LinkedAllowNull.java new file mode 100644 index 0000000..a26f0dd --- /dev/null +++ b/collections/linkedlist/linkedlist-in-java/LinkedAllowNull.java @@ -0,0 +1,25 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +/** + * A Java program to prove that LinkedList Allows Duplicates + * @author coderolls.com + * + */ +public class LinkedAllowNull { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("John"); + linkedList.add("Mark"); + linkedList.add("Mark"); + linkedList.add("Mark"); + linkedList.add("Ruby"); + linkedList.add("Lucy"); + linkedList.add("Vikram"); + linkedList.add("Vikram"); + + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/linkedlist-in-java/LinkedListInsertionOrder.java b/collections/linkedlist/linkedlist-in-java/LinkedListInsertionOrder.java new file mode 100644 index 0000000..14007ae --- /dev/null +++ b/collections/linkedlist/linkedlist-in-java/LinkedListInsertionOrder.java @@ -0,0 +1,22 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +/** + * A Java program to prove that LinkedList maintains insertion order + * @author coderolls.com + * + */ +public class LinkedListInsertionOrder { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + linkedList.add("John"); + linkedList.add("Mark"); + linkedList.add("Ruby"); + linkedList.add("Lucy"); + linkedList.add("Vikram"); + + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorExample.java b/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorExample.java new file mode 100644 index 0000000..7216f99 --- /dev/null +++ b/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorExample.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.ListIterator; +/** + * An example java program about the listIterator(int index) + * method of the linkedList. + * + * @author coderolls.com + */ +public class LinkedListListIteratorExample { + + public static void main(String[] args) { + + LinkedList states = new LinkedList<>(); + + // add state in the linkedList + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + //given index 3, iterator start from element with index 3 + ListIterator itr = states.listIterator(3); + System.out.println("Iterating the list from index 3\n"); + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} \ No newline at end of file diff --git a/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorException.java b/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorException.java new file mode 100644 index 0000000..4eeb2ad --- /dev/null +++ b/collections/linkedlist/linkedlist-listiterator/LinkedListListIteratorException.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; +import java.util.ListIterator; +/** + * An example java program about the listIterator() + * method of the linkedList. + * + * @author coderolls.com + */ +public class LinkedListListIteratorException { + + public static void main(String[] args) { + + LinkedList states = new LinkedList<>(); + + // add state in the linkedList + states.add("California"); + states.add("Texas"); + states.add("Montana"); + states.add("Arizona"); + states.add("Florida"); + states.add("Michigan"); + states.add("New Jersey"); + states.add("Washington"); + states.add("Ohio"); + + //given index 120, it will throw IndexOutOfBoundsException + ListIterator itr = states.listIterator(120); + + while(itr.hasNext()) { + String state = itr.next(); + System.out.println("The state :"+ state); + } + } +} \ No newline at end of file diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemove.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemove.java new file mode 100644 index 0000000..4c3e3a2 --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemove.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove head element from + * the linkedList using the remove() method. + * + * @author coderolls.com + * + */ +public class LinkedListRemove { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes head(first element) i.e. removes BHIM + linkedList.remove(); + + System.out.println("\nLinkedList after removing an element using remove() method:"); + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveFirst.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveFirst.java new file mode 100644 index 0000000..116fbff --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveFirst.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove first element from + * the linkedList using the removeFirst() method. + * + * @author coderolls.com + * + */ +public class LinkedListRemoveFirst { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes first element i.e. removes BHIM + linkedList.removeFirst(); + + System.out.println("\nLinkedList after removing an element using removeFirst() method:"); + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveLast.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveLast.java new file mode 100644 index 0000000..048b98a --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveLast.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove last element from + * the linkedList using the removeLast() method. + * + * @author coderolls.com + * + */ +public class LinkedListRemoveLast { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes first element i.e. removes BHIM + linkedList.removeLast(); + + System.out.println("\nLinkedList after removing an element using removeLast() method:"); + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObject.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObject.java new file mode 100644 index 0000000..53ac935 --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObject.java @@ -0,0 +1,33 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove the specified element from + * the linkedList using the remove(Object o) method. + * + * @author coderolls.com + * + */ +public class LinkedListRemoveObject { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes the specified element i.e. removes Freecharge + linkedList.remove("Freecharge"); + + System.out.println("\nLinkedList after removing an element using remove(Object o) method:"); + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObjectByIndex.java b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObjectByIndex.java new file mode 100644 index 0000000..23da316 --- /dev/null +++ b/collections/linkedlist/remove-element-from-linkedlist/LinkedListRemoveObjectByIndex.java @@ -0,0 +1,32 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A Java program to remove element at the specified position i.e. index + * from the linkedList using the remove(int index) method. + * + * @author coderolls.com + */ +public class LinkedListRemoveObjectByIndex { + + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList(); + + linkedList.add("BHIM"); + linkedList.add("PayTM"); + linkedList.add("Freecharge"); + linkedList.add("PhonePe"); + linkedList.add("JioPay"); + + System.out.println("LinkedList before removing an element:"); + System.out.println(linkedList); + + // removes the element at index 3 i.e. removes PhonePe + linkedList.remove(3); + + System.out.println("\nLinkedList after removing an element using remove(int index) method:"); + System.out.println(linkedList); + } +} diff --git a/collections/linkedlist/set-element-at-an-index-in-linkedlist/LinkedListSet.java b/collections/linkedlist/set-element-at-an-index-in-linkedlist/LinkedListSet.java new file mode 100644 index 0000000..251898b --- /dev/null +++ b/collections/linkedlist/set-element-at-an-index-in-linkedlist/LinkedListSet.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.LinkedList; + +import java.util.LinkedList; + +/** + * A java program to set an element at specified index + * using the set(int index, E element) method. + * + * @author coderolls.com + */ +public class LinkedListSet { + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + + linkedList.add("IN"); + linkedList.add("US"); + linkedList.add("FR"); + linkedList.add("JP"); + linkedList.add("CN"); + linkedList.add("RU"); + + System.out.println("LinkedList before setting an element at index 4:"); + System.out.println(linkedList); + + // replaces CN with BR + // returns the previously present element at index 4 i.e CN + String previousElement = linkedList.set(4, "BR"); + + System.out.println("\nLinkedList after setting an element BR at index 4:"); + System.out.println(linkedList); + + System.out.println("\nPreviously present element at index 4:"); + System.out.println(previousElement); + } +} diff --git a/java-basic/for-each-loop/ArrayForEachExample.java b/java-basic/for-each-loop/ArrayForEachExample.java new file mode 100644 index 0000000..8ab7142 --- /dev/null +++ b/java-basic/for-each-loop/ArrayForEachExample.java @@ -0,0 +1,23 @@ +package com.gaurav.ExProject.ArrayList; + +/** + * A java program to traverse the array using the + * for-each loop i.e. enhanced for loop + * + * @author Guarav Kukade at coderolls.com + * + */ +public class ArrayForEachExample { + + public static void main(String[] args) { + + // create an array of marks + int[] numbers = { 88, 95, 65, 76, 78, 45, 95, 96, 56}; + + // traversing the array using for-each loop + for(int number: numbers) { + + System.out.println("The number is "+ number); + } + } +} diff --git a/java-basic/for-each-loop/ArrayListForEachExample.java b/java-basic/for-each-loop/ArrayListForEachExample.java new file mode 100644 index 0000000..01daa0d --- /dev/null +++ b/java-basic/for-each-loop/ArrayListForEachExample.java @@ -0,0 +1,36 @@ +package com.gaurav.ExProject.ArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * A java program to traverse the collection using + * the for-each loop i.e. enhanced for loop + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class ArrayListForEachExample { + + public static void main(String[] args) { + + // create an empty arraylist of Integer + List numbers = new ArrayList(); + + //add number to the list + numbers.add(1); + numbers.add(8); + numbers.add(8); + numbers.add(5); + numbers.add(4); + numbers.add(6); + numbers.add(3); + numbers.add(2); + numbers.add(9); + + //traversing the collection using for-each loop + for(Integer number: numbers) { + System.out.println("The number is "+ number); + } + } +} diff --git a/java-programs/largest-element-in-an-array/LargestElementInArray.java b/java-programs/largest-element-in-an-array/LargestElementInArray.java new file mode 100644 index 0000000..a4bb4d9 --- /dev/null +++ b/java-programs/largest-element-in-an-array/LargestElementInArray.java @@ -0,0 +1,32 @@ +package com.coderolls.JavaPrograms; +/** + * A Java program to find the largest number in an array + * using for loop iteration. + * + * @author coderolls.com + * + */ +public class LargestElementInArray { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11}; + + int largestElement = getLargest(arr); + System.out.println("Largest element in an array 'arr' is :"+ largestElement); + + } + + private static int getLargest(int[] arr) { + int n = arr.length; + int largest = arr[0]; + + for(int i=0; iarr[0]) { + largest = arr[i]; + } + } + return largest; + } +} diff --git a/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java new file mode 100644 index 0000000..d061699 --- /dev/null +++ b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingArrays.java @@ -0,0 +1,28 @@ +package com.coderolls.JavaPrograms; + +import java.util.Arrays; + +/** + * A Java program to find the largest number in an array + * using Arrays.sort(). + * + * Arrays.sort() sort the array in natural sorting order + * + * @author coderolls.com + * + */ +public class LargestElementInArrayUsingArrays { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11}; + + int largestElement = getLargest(arr); + System.out.println("Largest element in an array 'arr' " + + "using Array.sort() is :"+ largestElement); + } + + private static int getLargest(int[] arr) { + Arrays.sort(arr); + return arr[arr.length-1]; + } +} diff --git a/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java new file mode 100644 index 0000000..311ee0d --- /dev/null +++ b/java-programs/largest-element-in-an-array/LargestElementInArrayUsingStream.java @@ -0,0 +1,27 @@ +package com.coderolls.JavaPrograms; + +import java.util.Arrays; +/** + * A Java program to find the largest number in an array + * using Java 8 Streams API. + * + * max() returns the maximum element of this stream + * + * @author coderolls.com + * + */ +public class LargestElementInArrayUsingStream { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11}; + + int largestElement = getLargest(arr); + System.out.println("Largest element in an array 'arr'" + + "using Java 8 Streams API is :"+ largestElement); + } + + private static int getLargest(int[] arr) { + int largest = Arrays.stream(arr).max().getAsInt(); + return largest; + } +} diff --git a/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java new file mode 100644 index 0000000..170298a --- /dev/null +++ b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArray.java @@ -0,0 +1,41 @@ +package com.coderolls.JavaPrograms.second_largest; +/** + * A Java program to find the second largest number in an array + * by iterating over an array using for loop. + * + * @author coderolls.com + */ +public class SecondLargestElementInArray { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11, 18, 13}; + + int secondLargest = getSecondLargest(arr); + System.out.println("The second largest element in " + + "an array 'arr'is :"+ secondLargest); + } + + private static int getSecondLargest(int[] arr) { + int n =arr.length; + int largest =arr[0]; + int secondLargest = -1; + + for(int i=0; ilargest) { + //if you found the new largest, + //copy current largest to second largest and + //copy current element arr[i] to largest + secondLargest = largest; + largest = arr[i]; + }else if(arr[i]!=largest) { + // if the current element arr[i] is not the largest and + // still larger than the current secondLargest + // then copy it to secondLargest + if(arr[i]>secondLargest) { + secondLargest = arr[i]; + } + } + } + return secondLargest; + } +} diff --git a/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java new file mode 100644 index 0000000..5e3aa18 --- /dev/null +++ b/java-programs/second-largest-element-in-an-array/SecondLargestElementInArrayUsingArrays.java @@ -0,0 +1,26 @@ +package com.coderolls.JavaPrograms.second_largest; + +import java.util.Arrays; + +/** + * A Java program to find the second largest number in an array + * using Arrays.sort() method. + * + * @author coderolls.com + */ +public class SecondLargestElementInArrayUsingArrays { + + public static void main(String[] args) { + int[] arr = {2, 5, 9, 8, 11, 18, 13}; + + int secondLargest = getSecondLargest(arr); + System.out.println("The second largest element in" + + "an array 'arr' is using Arrays.sort() :"+ secondLargest); + } + + private static int getSecondLargest(int[] arr) { + Arrays.sort(arr); + // return second largest, so length-2 + return arr[arr.length-2]; + } +} diff --git a/java-string/java-reverse-string-using-recursion/ReverseStringUsingRecursion.java b/java-string/java-reverse-string-using-recursion/ReverseStringUsingRecursion.java new file mode 100644 index 0000000..77a8f21 --- /dev/null +++ b/java-string/java-reverse-string-using-recursion/ReverseStringUsingRecursion.java @@ -0,0 +1,23 @@ +package ArrayListAddExample; +/** + * A java program to reverse a striong using recursion. + * + * @author gaurav + * + */ +public class ReverseStringUsingRecursion { + + public static void main(String[] args) { + + String str = "Hello"; + System.out.println(reverse(str)); + + } + + public static String reverse(String str) { + if ((null == str) || (str.length() <= 1)) { + return str; + } + return reverse(str.substring(1)) + str.charAt(0); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy