



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




double[ ] value = new double[4]; for ( int i = 0; i < 4; i++ ) value[i] = readDouble( ); for ( int i = 3; i >= 0; i-- ) System.out.println( value[i] ); public static double readDouble( ) { return Double.parseDouble( JOptionPane.showInputDialog( "Next value:" ) ); }
int[ ] cutOffs = { 90, 82, 75, 66 };
public static char letterGrade( int numeric ) { int[ ] cutOffs = { 90, 82, 75, 66 }; char[ ] letters = { 'A', 'B', 'C', 'D' }; for ( int i = 0; i < cutOffs.length; i++ ) { if ( numeric >= cutOffs[i] ) { return letters[i]; } } return 'F'; }
String: String str = "Hello"; Array of chars: char[ ] chr = "Hello"; // Illegal! Type mismatch. char[ ] chr = { 'H', 'e', 'l', 'l', 'o' };
Operation String char[ ] Result Get length str.length( ) chr.length 5 Get char str.charAt[4] chr[4] ‘o’ Set char (not possible) chr[0] = ‘J’; “Jello”
String str3 = new String( chr );
public static void squareValues( double[ ] v ) { for ( int i = 0; i < v.length; i++ ) v[i] = v[i] * v[i]; } double[ ] height = { 6.42, 7.10, 4.83 }; squareValues( height ); printDoubles( height );
Example: A static method, doubleSize, that is given an array b of char’s, creates a new array b2 of twice the size of the original. It copies the values of b to b2, and sets the remainder to ‘ ’. It returns b2. public static char[ ] doubleSize( char[ ] b ) { char[ ] b2 = new char[ 2 * b.length ]; for ( int i = 0; i < b.length; i++ ) b2[i] = b[i]; for ( int i = b.length; i < b2.length; i++ ) b2[i] = ' '; return b2; } Usage: char[ ] buffer = { 'J', 'a', 'v', 'a' }; char[ ] newBuffer = doubleSize( buffer );