Length intluckynumbersnewintlen forinti0ileni could

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: = new int[5]; theArray = new int {1, 2, 3, 4, 5}; // an “anonymous” array CPSC 324 ‐‐ Spring 2010 14 7 1/19/10 Java Arrays •  Array vars are really references (pointers) to arrays int smallPrimes = {2, 3, 5, 7}; smallPrimes = 2 3 luckyNumbers = “smallPrimes” holds a “reference” to the array in memory 5 7 8 •  This means we have to be careful when we copy (assign) one array to another array var int luckyNumbers = smallPrimes luckyNumbers[3] = 8; CPSC 324 ‐‐ Spring 2010 15 Copying Arrays •  One way to copy an array: int len = primeNumbers.length; int luckyNumbers = new int[len]; for(int i = 0; i < len; i++) Could foreach be used here? luckyNumbers[i] = primeNumbers[i]; •  Java provides helper functions for copying: int luckyNumbers = Array.copyOf(primeNumbers, len); •  For more Array helpers, see the online api...
View Full Document

Ask a homework question - tutors are online