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


by: Mallie Crist


Marketplace > Georgia State University > ComputerScienence > CSC 2310 > PRIN OF COMPUTER PROGRAMMING
Mallie Crist
GPA 3.62

Louis Henry

Almost Ready


These notes were just uploaded, and will be ready to view shortly.

Purchase these notes here, or revisit this page.

Either way, we'll remind you when they're ready :)

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

Louis Henry
Class Notes
25 ?




Popular in Course

Popular in ComputerScienence

This 4 page Class Notes was uploaded by Mallie Crist on Monday September 21, 2015. The Class Notes belongs to CSC 2310 at Georgia State University taught by Louis Henry in Fall. Since its upload, it has received 16 views. For similar materials see /class/209886/csc-2310-georgia-state-university in ComputerScienence at Georgia State University.

Similar to CSC 2310 at GSU

Popular in ComputerScienence




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: 09/21/15
Arrays Although you could write Java programs using what you39ve learned so far those programs would be pretty dull Much of the good stuff in Java or in any programming language results when you have arrays to store values in and control ow constructs loops and conditionals to execute different bits of a program based on tests Today you ll nd out about the following Arrays one of the most useful objects in Java which enable you to collect objects or prmitive types into an easytomanage list Block statements for grouping together related statements if and switch for conditional tests for and while loops for iteration or repeating a statement or statements multiple times Arrays Arrays in Java are different than they are in other languages Arrays in Java are actual objects that can be passed around and treated just like other objects Arrays are a way to store a list of items Each slot of the array holds an individual element and you can place elements into or change the contents or those slots as you need to Arrays can contain any type of element value primitive types or objects but you can t store different types in a single array You can have an array of integers or an array of strings or an array of arrays but you can t have an array that contains for example both strings and integers To create an array in Java you use three steps 1 Declare a variable to hold the may 2 Create a new array object and assign it to the array variable 3 Store things in that array Declaring Array Variables The rst step to creating an array is creating a variable that will hold the array just as you would any other variable Array variables indicate the type of object the array will hold just as they do for any variable and the name of the array followed by empty brackets The following are all typical array variable declarations String difficultWords Point hits int temps An alternate method of de ning an array variable is to put the brackets after the type instead of after the variable They are equivalent but this latter form is often much more readable So for example these three declarations could be written like this String difficultWords Point hits int temps Creating Array Objects The second step is to create an array object and assign it to that variable There are two ways to do this Using new Directly initializing the contents of that array The rst way is to use the new operator to create a new instance of an array String names new String 10 That line creates a new array of Strings with ten slots containing elements When you create a new array object using new you must indicate how many slots that array will hold Array objects can contain primitive types such as integers or booleans just as they can contain objects int temps new int99 When you create an array object using new all its slots are initialized for you 0 for numeric arrays false for boolean 390 for character arrays and null for objects You can also create and initialize an array at the same time Instead of using new to create the new array object enclose the elements of the array inside braces separated by commas String chiles quotjalapenoquot quotanaheimquot quotserranoquot quothabaneroquot quotthaiquot Each of the elements inside the braces must be of the same type and must be the same type as the variable that holds that array An array the size of the number of elements you39ve included will be automatically created for you This example creates an array of String objects named chiles that contains ve elements Accessing Array Elements Once you have an array with initial values you can test and change the values in each slot of that array To get at a value stored within an array use the array subscript expression myArraysubscript The myArray part of this expression is a variable holding an array object although it can also be an expression that results in an array The subscript expression speci es the slot within the array to access Array subscripts start with 0 as they do in C and C So an array with ten elements has ten array slots accessed using subscript 0 to 9 Note that all array subscripts are checked to make sure that they are inside the boundaries of the array greater than or equal to 0 but less than the array s length either when your Java program is compiled or when it is run It is impossible in Java to access or assign a value to an array slot outside of the boundaries of the array Note the following two statements for example String arr new String10 arrlO quoteggplantquot A program with that last statement in it produces a compiler error at that line when you try to compile it The array stored in arr has only ten slots numbered from 0 the element at subscript 10 doesn t exist and the Java compiler will check for that If the array subscript is calculated at runtime for example as part of a loop and ends up outside the boundaries of the array the Java interpreter also produces an error actually to be technically correct it throws an exception How can you keep from overrunning the end of an array accidentally in your own programs You can test for the length of the array in your programs using the length instance variableiit39s available for all array objects regardless of type int len arrlength returns 10 Changing Array Elements To assign an element value to a particular array slot merely put an assignment statement after the array access expression myarrayl 15 sentence0 quotThequot sentence10 sentenceOL An important thing to note is that an array of objects in Java is an array of references to those objects When you assign a value to a slot in an array you re creating a reference to that object just as you do for a plain variable When you move values around inside arrays as in that last line you just reassign the reference you don t copy the value from one slot to another Arrays of primitive types such as ints or oats do copy the values from one slot to another Arrays of references to objects as opposed to the objects themselves are particularly useful because it means you can have multiple references to the same objects both inside and outside arraysifor example you can assign an object contained in an array to a variable and refer to that same object by using either the variable or the array position


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

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!"

Janice Dongeun University of Washington

"I used the money I made selling my notes & study guides to pay for spring break in Olympia, Washington...which was Sweet!"

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."


"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.