Unit 4
Home-->Comp Sci
- Declaring Arrays
- type [] name=new type[size]
- numbered from zero to size-1
- make sure when assigning values that the element number is within the proper range
- Traversing an Array
- for(int x=0; a<array.length; x++)
- for two dimensional arrays nested for loops are used
- physical size is what it can hold and logical size is how much it does hold
- Two dimensonal arrays
- int[][] values=new itn[5][10];
- [rows][collums]
- by leaving the colums blank, their length can later be declared creating an ragged array
- Enhanced for loops
- for(int element:list)
- automatically visits and stores every item in the structure in order
- can not do
- move through the array in reverse
- assign elements to positions in the array
- track the position of the current element
- access any element other than the current item on each pass
- Arrays of Objects
- arrays can hold any type of refrence data or objects
- when instantiated, each element is null by default, and each position int eh array is a refrence to an object of that type
- Triangle[] list=new Triangle[10];
- to fill the array, an object must be instantiated for each position
- for(int x=0; x<list.length;x++)
list[x]=newTriangle();
- Arrays and methods
- a method can have an array as a parameter or a return type
- what is passed as a parameter is a refrence to an array, and if the method manipulates the array they are permanent changes
- when a method returns an array, most times it must instianate it