New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Week 6 Notes

by: Taylor Kahl

Week 6 Notes CSC 2310

Taylor Kahl
GPA 4.21

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

This covers the lecture from Wednesday Feb 17 (since we had a test on Monday). Includes material from the textbook chapter 10 on ArrayLists
Princliples of Computer Programming
Kebina Manandhar
Class Notes
25 ?




Popular in Princliples of Computer Programming

Popular in ComputerScienence

This 4 page Class Notes was uploaded by Taylor Kahl on Saturday February 20, 2016. The Class Notes belongs to CSC 2310 at Georgia State University taught by Kebina Manandhar in Winter 2016. Since its upload, it has received 57 views. For similar materials see Princliples of Computer Programming in ComputerScienence at Georgia State University.

Similar to CSC 2310 at GSU


Reviews for Week 6 Notes


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 02/20/16
From lecture on Wednesday, Feb 17: Exercise: Write a method intersect that accepts two sorted array lists of integers as parameters and returns a new list that contains only the elements that are found in both lists. My solution: public static ArrayList<Integer> intersect(ArrayList<Integer> list1, ArrayList<Integer> list2) { ArrayList<Integer> newList = new ArrayList<Integer>(); for (int i=0; i<list1.size(); i++) { if (list2.contains(list1.get(i))) { newList.add(list1.get(i)); } } return newList; } This method goes through every element in list1, then checks if that element is also an element in list2. If it is, the element is added to newList. Objects storing collections:  Remember that objects have fields, values stored within the object  Objects may store collections, such as Arrays and ArrayLists, as fields  Example: Course objects storing grades and students’ names public class Course { private double[] grades; //an Array of doubles private ArrayList<String> studentNames; //an ArrayList of Strings public Course() { //a constructor for new Course objects that initializes its fields grades = new double[4]; studentNames = new ArrayList<String>(); } } Sorting ArrayLists:  The method Collections.sort sorts ArrayLists o integers and doubles are sorted from least to greatest o Strings are sorted in alphabetical order  Collections.sort is part of the java.util package o has to be imported  Example of sorting: import java.util.*; public class ArrayListSorting { public static void main(String[] args) { ArrayList<String> stringList = new ArrayList<String>(); stringList.add("c"); stringList.add("b"); stringList.add("a"); stringList.add("d"); System.out.println("Before sorting: "+stringList); Collections.sort(stringList); System.out.println("After sorting: "+stringList); } } Output: Before sorting: [c, b, a, d] After sorting: [a, b, c, d] The Comparable Interface:  Sorting objects are possible using the Comparable interface o Ex: the String class implements the Comparable interface  But if we tried to use the Collections.sort method on Point objects, the program won’t compile o Point does not implement the Comparable interface  You can implement this method to compare objects of the classes you create  The Comparable interface looks something like this: public interface Comparable<E> { public int compareTo(E other); }  Any class that implements this interface must define the compareTo method o compareTo returns 3 possible integers:  negative to indicate less than  0 to indicate equal  positive to indicate greater than o Integers compare the quantity of values o Strings compare the alphabetical order of values o Examples calling the compareTo method:  if (x.compareTo(y) < 0) {  if (x.compareTo(y) != 0) {  Example: writing the Point class to implement the Comparable interface public class Point implements Comparable<Point> { //btw the <> you have to specify the type of object public int compareTo(Point other) { if (x < other.x) { return -1; } else if (x > other.x) { return 1; } else if (y < other.y) { return -1; } else if (y > other.y) { return 1; } else { return 0; } } } This compareTo method will compare Point objects based on their x and y values. First it will compare which x value is greatest. If they are equal, it will compare which y value is greatest. If they are also equal, the Points are equal ***Now that the Point class implements Comparable, we can use the Collections.sort method to sort an ArrayList<Point>  Other methods that use compareTo:  binarySearch – searches for the index of an Array where a particular value is o Example: finding the index of a String in an Array String[] a = {“a1”, “bob”, “cari”, “dan”, mike”}; int index = Arrays.binarySearch(a, “dan”); //index = 3  Tricks for more efficient compareTo methods:  Subtraction trick – subtracting related values gives you the desired result: a negative, a positive or 0 o Example: a revised compareTo method for Points: public int compareTo(Point other) { if (x != other.x) { return x – other.x; } else { return y – other.y; } } ***Note, this won’t work for double values because the compareTo method has a return type of int


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Anthony Lee UC Santa Barbara

"I bought an awesome study guide, which helped me get an A in my Math 34B class this quarter!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.