Arrays CS�180 15�Feb�2008
Announcements • Exam�1�Grades�on�Blackboard • Project�2�scores:�end�of�Class • Project�4�,�due�date:20 th Feb – Snakes�&�Ladders�Game • Review�of�Q5,Q8,Q11�&�Programming� Questions
Arrays:�What�they�mean? • An� array is�an�object�used�to�store�a� (possibly�large)�collection�of�data. • All�the�data�stored�in�the�array�must�be�of� the�same�type. • It��enables�you�to�use�a�single�name�to� represent�a�collection�of�items�&�refer�to� an�item�by�specifying�the�item�number�
Types�of�Arrays • One-Dimensional�Arrays – int students[�5] • Multi-Dimensional�arrays – Simplest�form�is�a�2-D�Array • int students[3�][�5] – Example�of�3-D�Array • int students[�5][7�][�3]
Creating�an�Array • Declare�an�array�– 2�forms – type�arrayname[�]; Ex:�int number[�]; – type�[�]�arrayname; Ex:�int [�]�number; • Create�memory�location – arrayname=�new�type[size]; – Ex:�number=�new�int[2]; – Here�we�have�created�an�array�of� type� integer�with�2� elements� and�named�it�as number� • Put�values� – arrayname[index]=value; – Ex:�number[0]=35; – Ex:�number[1]=40;
Diagram:�Creation�of�Array� Statement Result number int number[�]; points�nowhere number number=new�int[2]; • This�is�an�array�of� length�2 number�[0] • “number” is�a�reference� that�points�to�the�first� number�[1] memory�address Memory�locations
• Index�/�Subscript�of�Array� – int number�[i] – Here� “i” is�an�integer�used�to�refer�to�an�element� of�the�array� number .� It�is�called�the� Index • Ex:�number[0]�:�indicates�the�first�element�of� number • Ex:�number[1]�:�indicates�the�second�element�of� number • Length:�Instance�Variable • length is�a�public�instance�variable • The�length�variable�stores�the�number�of�elements�the� array�can�hold. • Using� Array_Name .length typically�produces�clearer�code� than�using�an�integer�literal.
Array�Terminology
Array�index�out�of�bounds • It�is�a�runtime�error�which�is�caused�by� incorrect�use�of�array�index. – Ex:�int number[]=new�int[2]; – number[0]=1; – number[1]=2; – number[2]=17;� Error�:�array�“number” has�just�2�elements!
Initializing�Arrays • An�array�can�be�initialized�at�the�time�it�is� declared. • example double[]�reading�=�{3,�3,�15.8,�9.7}; –The�size�of�the�array�is�determined�by� the�number�of�values�in�the� initializer list .
Initializing�Arrays,�cont. • Uninitialized array�elements�are�set�to�the� default�value�of�the�base�type.� • However,�it’s�better�to�use�either�an� initializer list�or�a� for loop. int[]�count�=�new�int[100]; for�(int i�=�0,�i�<�count.length,�i++) { count[i]�=�0; }
Arrays�in�Classes�&�Methods • Arrays�can�be�used�as�instance�variables� in�classes. • Both�an�indexed�variable�of�an�array�and� an�entire�array�can�be�a�argument�of�a� method. • Methods�can�return�an�indexed�variable�of� an�array�or�an�entire�array.
Arrays�in�Classes�&�Methods� Example class�cs180Class { String�studentName[],�firstName[]; Array�as�instance�variables public�cs180Class() { System.out.println("enter number�of�students:"); int num_students=keyboard.nextInt(); firstName =�new�String[num_students]; ... studentName =�setData(firstName); } Array�as�argument�of�a�method: String[]�setData (String�name[�]) Call�by�Value { String[]�temp�=�new�String�[name.length]; for(int i=0;�i�<�name.length;�i++) { temp[i]�=�name[i]; } Returning�an�Array return�temp; } }
What�about� Main method? • Recall�the�heading�for�method� main : public�static�void�main(String[]�args) An�array�of� String values�can�be�provided�in�the�command� line. example java�TestProgram Mary�Lou – args[0] is�set�to “Mary” – args[1] is�set�to “Lou” System.out.println(“Hello “ +�args[0]�+�� “ “ +�args[1]); prints Hello�Mary�Lou.
Use�of�=�&�==�with�Arrays – The�assignment�operator�creates�an�alias,� not a�copy�of�the�array. – The�equality�operator�determines�if�two� references�contain�the�same�memory� address,� not if�two�arrays�contain�the�same� values.
Making�a�Copy�of�an�Array • example int[]�a�=�new�int[50]; int[]�b�=�new�int[50]; ... for�(int j�=�0;�j�<�a.length;�j++) b[j]�=�a[j];
Determining�the�“Equality” of�Two� Arrays • To�determine�if�two�arrays�at�different� memory�locations�contain�the�same� elements�in�the�same�order,�define�an� equals method�which�determines�if –both�arrays�have�the�same�number�of� elements –each�element�in�the�first�array�is�the� same�as�the�corresponding�element�in� the�second�array.
Swapping�Array�elements • class�interchange
Uses�of�Arrays • Searching�an�element�in�a�list�of�elements • Sorting – Ascending�order – Descending�order – Alphabetic�order
Example:�Selection�Sort • Selection�sort�begins� by�finding�the� smallest�item�in�the� array�and�swapping�it� with�the�item�in� a[0] .
Selection�Sort:�class class�SelectionSort
Multi-Dimensional�Arrays • 2-Dimensional�Array – A�row�&�Column�structure – int [�]�[�]�world=�new�int[10][9] – Indexing�can�be�done�using�2�integers:�i,�j • world�[i]�[j]�:�refers�to�element�at� row�i�and�column�j • Length�:�2-D�array – world�.length :�returns�the�#�of�rows – world�[i]�.length :�returns�the�#�of�columns�in� row�‘i’
2-Dimensional�Array Salary�information� of�10�departments� each�having�6� employees 2-D�representation double[�][�]�Salary=�new�double[10][6]; Department�10,�Employee�1: Salary[9][0]
Initialize�2-D�Arrays • You�can�initialize�by� nesting�of�loops final�int MAX_ROW�=�25; final�int MAX_COL�=�1000;�� double[][]�experData =�new�double[MAX_ROW][MAX_COL]; int i,j; for�(i�=�0;�i�<�MAX_ROW;�i�++)�� {���� for�(j�=�0;�j�<�MAX_COL;�j�++)��� {� experData [i][j]�=�0.0;���� 2�rows�,�4�columns } }� • Alternate�way: – double�[][]�scores={ {5.3,�6.9,�8.8,�1.7}, {2.2,�9.4,�1.6,�7.5} };�
Ragged�Arrays • Since�a�two-dimensional�array�in�Java�is�an� array�of�arrays,�each�row�can�have�a�different� number�of�elements�(columns). • Arrays�in�which�rows�have�different�numbers�of� elements�are�called� ragged�arrays. • Example int[][]�b�=�new�int[3][]; b[0]�=�new�int[5];�//�array�of�5�elements b[1]�=�new�int[7];//�array�of�7�elements b[2]�=�new�int[4];�//�array�of�4�elements
Quiz • Task�:�print�all�the� system.out.println statements. public�class�arrayTest { public�static�void�main(String[]�args)�{ String[]�names={"Hemant",�"David",�"Wini","Kaku"}; System.out.println("length of�the�array:"�+�names.length); for(int i=0;i<names.length;i++) { System.out.println(names[i]); } }� }
Review:�Exam�1�– Q5 Q5.�Given�the�following�code: Q5.� Modified�Version double�a�=�1/3; double�b�=�1/3; .�Given�the�following�code: double�c�=�1/3; double�sum�=�a�+�b�+�c; double�a�=�1/3; The�value�of�variable�sum�is: double�b�=�1/3; (a)�0.9999 double�c�=�1/3; (b)�0.0 double�sum�=�a�+�b�+�c; (c)�0.999999999999999999 (d)�1.0 The�value�of�variable�sum�is: – All�integers:��0 – All�floats�0.0 – All�doubles:��0.0
Recommend
More recommend