length index Systemoutprint xindex Systemoutprintln class ArrayDemo public

Length index systemoutprint xindex systemoutprintln

This preview shows page 20 - 36 out of 43 pages.

for ( int index=0; index < x.length; index++ ) System.out.print( x[index] + " " ); System.out.println(); } } class ArrayDemo { public static void main ( String[] args ) { ArrayOps operate = new ArrayOps(); int[] ar1 = { -20, 19, 1, 5, -1, 27, 19, 5 } ; System.out.print ("\nThe array is: " ); operate.print( ar1 ); } } >> Lesson 3 2 0
Image of page 20

Subscribe to view the full document.

Example 4 class ChangeArray { void print ( int[] x ) { for ( int j=0; j < x.length; j++ ) System.out.print( x[j] + " " ); System.out.println( ); } void zeroElt ( int[] x, int elt ) { if ( elt < x.length ) x[ elt ] = 0; } } >> Lesson 3 2 1 class ChangeTest { public static void main ( String[] args ) { ChangeArray cng = new ChangeArray(); int[] value = {27, 19, 34, 5, 12} ; System.out.println( "Before:" ); cng.print( value ); cng.zeroElt( value, 0 ); System.out.println( "After:" ); cng.print( value ); } } OUTPUT : Before: 27 19 34 5 12 After: 0 19 34 5 12
Image of page 21
Another option to traversing an array 22
Image of page 22

Subscribe to view the full document.

MULTI-DIMENSIONAL ARRAY Lesson 4 >> Lesson 4 2 3
Image of page 23
2D Array Declaration : int twoD[ ][ ] = new int[4][5] ; >> Lesson 4 2 4
Image of page 24

Subscribe to view the full document.

COLLECTING OBJECTS Lesson 5 >> Lesson 5 2 5
Image of page 25
Array Array is a primitive data structure However, we can use array to store objects. Array is not expandable The size is fixed and cannot be changed during runtime. >> Lesson 5 2 6
Image of page 26

Subscribe to view the full document.

Collection Collection is a term used in Java to store a set of objects in structured manner. Why use Collection? Reduce programming effort Expandable array Mapping key and values Impose some constraint alphabetical order, does not allow duplicate. >> Lesson 5 2 7
Image of page 27
Collection API Collection is stored in java.util . Therefore, to use any data structure in collection we need to import this package. >> Lesson 5 2 8 BITP 3113 Object Oriented Programming Sem I 2012/2013 Auhtored by Emma McKay-Fikri
Image of page 28

Subscribe to view the full document.

Collection Structure List To store and manipulate a sequence of objects. May contain duplicate object Set To store and manipulate a sequence of objects. Keep things in order (this is not a guarantee) Does not allow duplicate Map To store key and value of a mapping This is not covered in BITP 3113 >> Lesson 5 2 9
Image of page 29
IT IS A LONG LIST Lesson 6 >> Lesson 6 3 0
Image of page 30

Subscribe to view the full document.

Using List >> Lesson 6 3 1 We are going to use one of these classes
Image of page 31
Common Class to Use From List ArrayList and Vector, behaves like an expandable array. Like an array, a Vector contains elements that are accessed using an integer index . However, unlike an array, the size of a Vector will expand if needed as items are added to it. Which one is better? Depends if your application is thread concern. ArrayList : Not thread-safe Vector : Thread-safe >> Lesson 6 3 2
Image of page 32

Subscribe to view the full document.

How to Use Vector? import java.util.Vector; public class DemoVector{ public static void main(String args[]) { Vector<String> fruits = new Vector<String>(); fruits.add("rambutan");fruits.add("jackfruit"); fruits.add("mangoesteen"); System.out.println(fruits); fruits.remove("jackfruit");fruits.add("mango"); System.out.println(fruits); System.out.println("mangoesteen is exist? " + fruits.contains("mangoesteen") ); } } >> Lesson 6 3 3 You need to download Vector.pdf from ulearn .
Image of page 33
Output 34
Image of page 34

Subscribe to view the full document.

SET Lesson 7 >> Lesson 7 3 5
Image of page 35
Image of page 36

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

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes