Created
December 30, 2012 09:49
-
-
Save adrianlee/4411822 to your computer and use it in GitHub Desktop.
An ArrayList, or a dynamically resizing array, is an array that resizes itself as
needed while still providing O(1) access. A typical implementation is that when a
vector is full, the array doubles in size. Each doubling takes O(n) time, but
happens so rarely that its amortized time is still O(1).
This file contains hidden or 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
| /* | |
| An ArrayList, or a dynamically resizing array, is an array that resizes itself as | |
| needed while still providing O(1) access. A typical implementation is that when a | |
| vector is full, the array doubles in size. Each doubling takes O(n) time, but | |
| happens so rarely that its amortized time is still O(1). | |
| public ArrayList<String> merge(String[] words, String[] more) { | |
| ArrayList<String> sentence = new ArrayList<String>(); | |
| for (String w : words) sentence.add(w); | |
| for (String w : more) sentence.add(w); | |
| return sentence | |
| } | |
| */ | |
| import java.util.*; | |
| import java.io.*; | |
| public class ArrayListMerge { | |
| public static ArrayList<String> merge(String[] words, String[] more) { | |
| ArrayList<String> sentence = new ArrayList<String>(); | |
| for (String w : words) { | |
| sentence.add(w); | |
| } | |
| for (String w : more) { | |
| sentence.add(w); | |
| } | |
| return sentence; | |
| } | |
| public static void main(String[] args) { | |
| String[] words = {"asd", "asd", "asd"}; | |
| String[] more = {"qwe", "qwe", "qwe"}; | |
| System.out.println(merge(words, more)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment