Java i lecture_8
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Java i lecture_8

on

  • 203 views

 

Statistics

Views

Total Views
203
Views on SlideShare
203
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters
  • White Space Characters

Java i lecture_8 Presentation Transcript

  • 1. Java I--Copyright © 2000 Tom Hunter
  • 2. Java I--Copyright © 2000 Tom HunterChapter 7Arrays
  • 3. Java I--Copyright © 2000 Tom Hunter• An Array consists of data items of the same type,all with the same name.• The size of an Array is “static”.• Once you have created an “array”, you cannotchange the number of elements it holds.• If you create an array with 42 occurrences, then itwill always have 42 elements.An Array is A “Data Structure”
  • 4. Java I--Copyright © 2000 Tom Hunter• If you wish to create an array where the numberof elements can be changed, you use classVector. (Not covered in Java I)An Array is A “Data Structure”
  • 5. Java I--Copyright © 2000 Tom Hunter• In Java, an array is a group of contiguous memorylocations that all have the same name and same type.• This declares that x is an array of char variables.char[] x;An Array is A “Data Structure”
  • 6. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”• The statement below defines a reference.char[] x;• Right now, I don’t know how many of them there’sgoing to be.X
  • 7. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”• Allocating the array decides the number of elements.x = new char[5];• If you remember “x” is a reference, the syntax above ismore understandable.X
  • 8. Java I--Copyright © 2000 Tom HunterWhen you allocate an array, the elements areautomatically initialized.Primitives:• numeric primitives are zeroed,• char primitives are made spaces,• boolean primitives are made falseReferences:For an array of any other type--the“references” are made null.
  • 9. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”x[0]= ‘Z’;• This assigns the char ‘Z’ to the first array position.XZ
  • 10. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”x[1]= ‘q’;• This assigns the char ‘q’ to the second array position.XZ q
  • 11. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”ZqPmRtKTAx[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7]x[8]Notice how the firstelement is the“Zeroth.”“Z” is the firstelement, with asubscript of “0”
  • 12. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”ZqPmRtKTAx[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7]x[8]• To change an element, youassign it this way.X[3] = ‘Y’;• This changes the value of thearray location, not the subscriptvalue.• If your array is declared witha data type of char, then youmust use single quotes.• Double quotes are used forString objects.
  • 13. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”ZqPmRtKTAx[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7]x[8]• The compiler will complainif you try to assign a double-quoted String to a char array.X[0] = “Z”;“Incompatible type:Cant convert java.lang.Stringto char.x[0] = "T";^1 error”
  • 14. Java I--Copyright © 2000 Tom Hunter• An array is a full-fledge object.• You can declare an array either of two ways:char[] x;orchar x[];An Array is A “Data Structure”• Either wayworks just as well.
  • 15. Java I--Copyright © 2000 Tom Hunter• If you put the brackets on the data type, you candeclare multiple references as all being the sametype of array:char[] x, y, z;• In this case, x, y and z are all declared as chararrays.An Array is A “Data Structure”
  • 16. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”• To learn the number of elements in the array,you call a property called “length.” Forexample:int y = 0;int[] x;x = new int[5];y = x.length;• Notice, “length” is NOT a method, it is aproperty.
  • 17. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”• You can use an “Initializer List” enclosed in bracesto quickly insert values in your array:int n[] = { 12, 44, 98, 1, 28 };• Because this list has 5 values in it, the resultingarray will have 5 elements.
  • 18. Java I--Copyright © 2000 Tom HunterAn Array is A “Data Structure”• If we had a char array, it would be like this:char m[] = { ‘t’, ‘I’, ‘M’, ‘Y’ };
  • 19. Java I--Copyright © 2000 Tom HunterConstant Variables & the“final” Qualifier• When used in a variable’s declaration, thekeyword “final” indicates that thevariable can never be changed.final int ARRAY_SIZE = 10;
  • 20. Java I--Copyright © 2000 Tom HunterConstant Variables & the “final” Qualifier• If you have chosen to make your variable“final” then you must initialize it in thesame statement that declares it--you have noother alternative.final int ARRAY_SIZE;ARRAY_SIZE = 10;
  • 21. Java I--Copyright © 2000 Tom HunterCall By Reference/Value• When we call a method and pass themethod a primitive-data-type variable, weare always passing a copy of the original,not the original.• Thus, if the method changes the variable itreceives, only the copy is changed--not theoriginal.
  • 22. Java I--Copyright © 2000 Tom HunterX3int x = 3;x is a primitive variable.If we call a method with x asan argument, we make a copyof x and the original copy isnot affected by any changeswe make in the copy.
  • 23. Java I--Copyright © 2000 Tom HunterX3y3int x = 3;yMethd( x )public int yMethd( inty ){return y++;}If I pass x to a method, a copy y ismade and any changes happen to thecopy.
  • 24. Java I--Copyright © 2000 Tom HunteroutNow, we have char[] out = {‘H’,’e’,’l’,’p’};If we pass the reference out to a method, we again pass acopy, but the copy points back to the original, and so ourmethod can change the original.H e l p
  • 25. Java I--Copyright © 2000 Tom HunterH e l poutdatachar[] out = {‘H’,’e’,’l’,’p’};strMethd( out );public void strMethd( char[] data ){data[3] = { ‘l’ };}
  • 26. Java I--Copyright © 2000 Tom Hunteroutdatachar out = {‘H’,’e’,’l’,’p’};strMethd( out );public void strMethd( char[] data ){data[3] = { ‘l’ };}H e l p
  • 27. Java I--Copyright © 2000 Tom Hunteroutdatachar out = {‘H’,’e’,’l’,’p’};strMethd( out );public void strMethd( char[] data ){data[3] = { ‘l’ };}H e l l
  • 28. Java I--Copyright © 2000 Tom HunteroutdataH e l l
  • 29. Java I--Copyright © 2000 Tom Hunter
  • 30. Java I--Copyright © 2000 Tom HunterPassing An Array to a Method• As you saw in the previous example, when wewish to pass an array to a method, the array’ssignature must be expecting to receive an array.• Thus, the method was declared:public void strMethd( char[] data ){}
  • 31. Java I--Copyright © 2000 Tom HunterPassing An Array to a Method• The method was clearly designed to receive a chararray, which it names data.public void strMethd( char[] data ){}• But when we called the method, we only used thebare name of the array we were sending. We didn’tuse the square brackets.strMethd( out );
  • 32. Java I--Copyright © 2000 Tom HunterPassing An Array to a Method• If we pass an entire array--meaning just the arrayname without any square brackets--then we arepassing the reference and the original array can bechanged in the method.• But, if we pass just one array value (and includethe square array brackets) then we are just passinga copy and the original cannot be changed.
  • 33. Java I--Copyright © 2000 Tom HunterPassing An Array to a Method• If we pass just the naked array name, the originalarray can be accessed and changed.char[] out;...wholeArray( out )public void wholeArray( char[] data ){data[2] = ‘L’;// will change original array}
  • 34. Java I--Copyright © 2000 Tom HunterPassing An Array to a Method• If we pass just a single array value, we can’tchange the original table.’char[] out;...pieceOfArray( out[2] )public void pieceOfArray( char d ){d = ‘x’;// won’t change original array}
  • 35. Java I--Copyright © 2000 Tom Hunter• If we pass just a single array value, we can’tchange the original table. The single array value ispassed by value.char[] out;...pieceOfArray( out[2] )public void pieceOfArray( char datum ){datum = ‘W’;// won’t change original array}Passing An Array to a MethodWhen we pass thesingle element,we include thesquare brackets.When the method’s argument isreceived, it is not recognized as an arrayelement. At this point, the method onlysees it as a single variable that waspassed by value.w
  • 36. Java I--Copyright © 2000 Tom Hunterpublic void start(){char[] out = { H, e, l, p };display.append( "Before out=" + out[0] + out[1] );wholeArray( out );pieceOfArray( out[1] );display.append( "nAfter out=" + out[0] + out[1]+ out[2] + out[3] );}public void wholeArray( char[] data ){char change = l;data[3] = change;display.append( "nIn wholeArray: " + data[0] + data[1]+ data[2] + data[3] );}public void pieceOfArray( char datum ){datum = W;}
  • 37. Java I--Copyright © 2000 Tom HunterPassing An Array to a MethodThe call using the entire array wasable to change the original.The call using just a single elementwas not able to change the original.
  • 38. Java I--Copyright © 2000 Tom HunterDouble-Subscripted Arrays• This is the syntax for a double-subscripted arraychar demo[][]• Java does not directly support two-dimensionalarrays--however, it lets you define an array whereeach element is an array--thereby achieving thesame effect.