Objectives
- Compare and contrast an
int[]
and anArrayList
ofInteger
int[]
and an ArrayList
of Integer
registeredCourses
, with a set of course numbers: 1010, 1020, 2080, 2140, 2150, 2160registeredCourses
into a new, larger array, updatedCourses
, and adds the course to this new arrayupdatedCourses
, the array that contains the newly-added courseupdatedCourses
contains a specific course number public class CourseNumbersArray {
public static void main(String[] args) {
int[] registeredCourses = {1010, 1020, 2080, 2140, 2150, 2160};
System.out.print("Originally registered for: ");
for(int course : registeredCourses){
System.out.print(course + ", ");
}
int[] updatedCourses = new int [registeredCourses.length + 1];
System.arraycopy(registeredCourses, 0, updatedCourses, 0, registeredCourses.length);
updatedCourses[registeredCourses.length] = 2280;
System.out.print("\nUpdated courses: ");
for(int course : updatedCourses){
System.out.print(course + ", ");
}
int courseCode = 2140;
boolean found = false;
for(int course : updatedCourses){
if(course == courseCode){
found = true;
}
}
if(found){
System.out.println("\nYou are registered for " + courseCode);
}
else{
System.out.println("You are not registered for " + courseCode);
}
}
}
.contains()
should simplify the search for a specific course.Originally registered for: 1010, 1020, 2080, 2140, 2150, 2160,
Updated courses: 1010, 1020, 2080, 2140, 2150, 2160, 2280,
You are registered for 2140