Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HelloWorld{
- public static void main(String []args){
- System.out.println("Hello World, Arrays");
- //Object[] definition = (2 ways to define an array)
- //Implicit = {def, def, def, def}
- int[] a = {5, 4, 12, 10};
- //Explicit = new int[elements/length]
- int[] b = new int[10];
- /*
- The array A has 4 elements, but a index ranging from 0-3
- if we said a[3] = 5; Then the 10 would be replaced with a 5.
- the array b has no elements defined, but has space for 10.
- Meaning the index is avaliable from 0-9. We can define a
- point on the array by b[5] = 6;
- To redefine a element in an array.
- Array[Index] = Definition;
- Array capacity is limited. A can never be modified to hold more than
- 4 elements and b can never be modified to hold more than 10 elements.
- */
- b[9] = 5; //defines the 10th element but the 9th index to 5;
- a[2] = 1; //defines the 3nd element but the 2th index to
- //Causes exception b[10] = 5; There is no 11th element or the
- //10th index
- //looping through an ArrayList
- for (int i = 0; i < a.length; i++) { //i ~ Index
- System.out.println(a[i]);
- }
- //Another way to loop. Limit no built in counter (i)
- for(int i : a) {
- System.out.println(i);
- }
- loopThroughArray(a); //Passes the Array A as an argument to the
- //function loopThroughArray to the parameters
- }
- public static void loopThroughArray(int[] array) {
- for(int i : array) {
- System.out.println("Looping: " + i);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement