Created
December 24, 2021 08:28
-
-
Save gauravkukade/a177a9371b6d9bf117cc4201efc7856f to your computer and use it in GitHub Desktop.
A Java program to add all elements of the specified collection to the ArrayList from a particular index. Read more at https://coderolls.com/arraylist-addall-method-in-java/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<String> entrepreneurs1 = new ArrayList<String>(); | |
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<String> entrepreneurs2 = new ArrayList<String>(); | |
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Entrepreneurs collection 1: | |
[Bill Gates, Steve Jobs, Jeff Bezos, Richard Branson, Mark Cuban] | |
Entrepreneurs collection 2: | |
[Mark Zuckerberg, Larry Page, Larry Ellison] | |
Entrepreneurs : | |
[Bill Gates, Steve Jobs, Mark Zuckerberg, Larry Page, Larry Ellison, Jeff Bezos, Richard Branson, Mark Cuban] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment