Lecture 4_ Arrays.pptx - Lecture 4 Arrays in Java...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 4: Arrays
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
[ ] in Java Constructors int[] arr = new int[5]; ■ // specify size, all vals initialized to 0 int[] arr = new int[] {1, 2, 3}; ■ // specify size and initialized elements int[][] arr = new int[5][6]; ■ 2D array, row by column arr.length
Image of page 2
Arrays class Arrays.sort([] arr) sorts the array by ascending order Arrays.sort([] arr, int fromIndex, int toIndex) sorts the array by ascending order starting with fromIndex to toIndex Arrays.toString([] arr) gets a string format of the string, e.g. “[1, 2, 3]” Arrays.binarySearch([] arr, val)
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Arrays vs ArrayLists (Java) Array Fixed size Can be multidimensional ArrayList Dynamic size Cannot hold primitive types (autoboxing) - only objects Resizing the arraylist takes time - O(n) to copy over all n elements
Image of page 4
ArrayLists ArrayList<E> arr = new ArrayList<E>(); can initialize with size, or with all elements of an existing Collection boolean add(E) appends to end of list, O(1) amortized runtime can also add at a specific index, O(n) runtime void clear() empty the list, O(n) runtime (initialize another array?) boolean contains(E) check if list contains element, O(n) runtime E get(int index) get the val at a specified index, O(1)
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lists in Python a = [], a = [1, 3, 5] can intialize empty or with elements list.append(x) appends to end of list, O(1) amortized runtime list.insert(i, x) insert item x at index i, O(n) runtime list.remove(x) remove first item in list that is equal to x, O(n) runtime list.pop(i) remove item from index i and return it, list.sort() sort list (can be customized) list.reverse() reverse list in place
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern