Storing items with arrays

From the course: Java Essential Training for Students

Start my 1-month free trial
  • Course details

    Taking a college-level programming course? Maximize your learning with these Java tutorials. Peggy Fisher explores command-level programming, basic techniques such as managing loops and creating methods, debugging Java code, working with classes and objects, and storing and sorting arrays. Along the way, she investigates the Java API and Java's capabilities for running simulations and algorithm analysis, and issues challenges to write programs that utilize all of these Java features.

    Instructor

    • Click here to view Peggy Fisher’s instructor page

      Peggy Fisher

      Content Strategist, Software Development Languages at Linkedin Learning Solutions with Lynda.com content

      Peggy Fisher is a programmer and content manager at LinkedIn Learning.

      Peggy's main focus is application programming in Java, Arduino, and C++. She has also worked on courses in COBOL and discrete ,athematics. Previously she was a faculty member at Penn State University's College of Information Sciences and Technology. She started out as a programmer working for a large insurance company, but after 18 years she left her job as a director of information technology to pursue her true passion teaching. She earned a master's degree in math education, and went on to teach high school math and computer science in Pennsylvania. In 2012, Peggy accepted a position as an instructional designer at Penn State, and shortly thereafter began teaching Intro to Application Programming with Java. She is a strong supporter of women in STEM. As one of the few female programming teachers, she serves as a mentor to incoming female freshmen who are considering a career in programming. She was also the K–12 outreach coordinator for the college, where she scheduled, ran, and taught summer camps for middle school and high school students. In a PBS NewsHour interview, she expressed that all students should take at least one programming class either in high school or college. Peggy enjoys constantly learning and finding new and exciting ways to bring technology to life in and outside of the classroom, such as using Arduino microcontrollers or Lego Mindstorms, to help make learning hands-on and fun.

    Skills covered in this course

  • Welcome

    - At this point you're probably already familiar with arrays, but let's go over some of the basics. An array is a container object that holds a fixed number of values of a single data type. The length of an array is established when the array is created. Once the array is created the length cannot be changed. To reference elements in an array we use an index value. That index value represents the location of the element in the array. The index values always start at zero and go to length -1. So if you have 10 elements, the first element is always gonna be in position zero. And if there's 10, the last one will be in position 9. Because you have 9 places plus the position zero, makes 10. The values in the array are stored in consecutive memory locations. The syntax for an array includes the data type[ ] = new data type[size]. Or, you can specify an initial list of values to be included in the array, and the size will be calculated automatically. There is one caution, beware of the out-of-bounds error. Since arrays are a fixed size, if you try to access data at an index value greater than the size of the array, your program will end abruptly during execution. This is not checked ahead of time by the compiler. But remember our section on error handling, that it might be helpful to add a try/catch for an index out-of-bounds error. Arrays can contain any of the primitive data types. Let's take a look at some examples. I have a class called Array Examples, and inside my class I just created a few sample arrays, just to review. The first one on line 13 is an integer array. I start by declaring the data type int, and then I have an [ and a ]. The name of my array, in this case I just called it "ages", is equal to a new int array, with 20 values. Remember that an array is a container object, and just like when we created classes in objects, we had to instantiate out objects by using the keyword "new", the same thing applies to arrays. So when you're creating a new array, such as int[ ] ages = you have to say "new int". When you declare an array and you give it a list of values, you don't have to use the keyword "new". So on line 14, I have a double array with my square brackets called "prices", and it contains four values: 5.25, 6.50. 2.30. and 10.75. The length of that array will be four, but remember the array index value starts at zero and goes to one less than the length. So starting on line 16, I wanted to add the values together, so I have double total = prices, at position 0 plus prices at position 1, prices at position 2, and finally, prices at position 3. If I tried to access prices at position 4, I was would get an array index out-of-bounds error. The next array is actually a Boolean array. You can create an array that holds a list of values that are either true or false. I name this array "responses" and I just set it equal to "true, false, false, true, true". We can also have character arrays. For example, it might be handy to have an array that includes all the vowels. So I have character [ ] vowels = {"a", "e", "i", "o", "u"}. Note there are single quotes around each vowel because this is a character array, not a string array. And the last array is an example if you wanted to have an array that can hold 10 names, you might declare String [ ] names = new String [10]; This program contains of the examples of the types of arrays that you might want to create.

  • Practice while you learn with exercise files

    Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

    Download the exercise files for this course. Get started with a free trial today.

  • Download courses and learn on the go

    Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

    Watch this course anytime, anywhere. Get started with a free trial today.

Contents