0% found this document useful (0 votes)
31 views

PBO - Week 04 - Array Progs

The document discusses initializing and manipulating arrays in Java. It shows how to create and initialize single-dimensional and multi-dimensional arrays, pass arrays to methods by reference and by value, and modify array elements within methods.

Uploaded by

Ariq Naufal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

PBO - Week 04 - Array Progs

The document discusses initializing and manipulating arrays in Java. It shows how to create and initialize single-dimensional and multi-dimensional arrays, pass arrays to methods by reference and by value, and modify array elements within methods.

Uploaded by

Ariq Naufal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1

// Fig. 7.3: InitArray.java


// Creating an array.

// Java extension packages


import javax.swing.*;

public class InitArray {

// main method begins execution of Java application


public static void main( String args[] )
{

// dynamically allocate array


int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int total = 0;
String output = "Subscript\tValue\n";

// append each array element's value to String output


for ( int counter = 0; counter < array.length; counter++ )
{
total += array[ counter ];
output += counter + "\t" + array[ counter ] + "\n";
}

output+="\n"+"And The Sum is: "+total ;

JTextArea outputArea = new JTextArea();


outputArea.setText( output );

JOptionPane.showMessageDialog( null, outputArea,


"Initializing an Array of int Values",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );
}
}

Java Week 04: Arrays


2

// Fig. 7.10: PassArray.java


// Passing arrays and individual array elements to methods

// Java core packages


import java.awt.Container;

// Java extension packages


import javax.swing.*;

public class PassArray extends JApplet {

// initialize applet
public void init()
{
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add( outputArea );

int array[] = { 1, 2, 3, 4, 5 };

String output =
"Effects of passing entire array by reference:\n" +
"The values of the original array are:\n";

// append original array elements to String output


for ( int counter = 0; counter < array.length; counter++ )
output += " " + array[ counter ];

modifyArray( array ); // array passed by reference

output += "\n\nThe values of the modified array are:\n";

// append modified array elements to String output


for ( int counter = 0; counter < array.length; counter++ )
output += " " + array[ counter ];

output += "\n\nEffects of passing array " +


"element by value:\n" +
"a[3] before modifyElement: " + array[ 3 ];

// attempt to modify array[ 3 ]


modifyElement( array[ 3 ] );

output += "\na[3] after modifyElement: " + array[ 3 ];


outputArea.setText( output );

} // end method init

// multiply each element of an array by 2


public void modifyArray( int array2[] )
{
for ( int counter = 0; counter < array2.length; counter++ )
array2[ counter ] *= 2;
}

// multiply argument by 2
public void modifyElement( int element )
{
element *= 2;
}

} // end class PassArray

Java Week 04: Arrays


3

// Fig. 7.15: InitArray2.java


// Initializing multidimensional arrays

// Java core packages


import java.awt.Container;

// Java extension packages


import javax.swing.*;

public class MyInitArray extends JApplet {


JTextArea outputArea;

// set up GUI and initialize applet


public void init()
{
outputArea = new JTextArea();
Container container = getContentPane();
container.add( outputArea );

int array1[][] = { { 1, 2, 3 }, { 4, 5, 6 } };
int array2[][] = { { 1, 2 }, { 3 }, { 4, 5, 6 } };

outputArea.setText( "Values in array1 by row are\n" );


buildOutput( array1 );

outputArea.append( "\nValues in array2 by row are\n" );


buildOutput( array2 );
}

// append rows and columns of an array to outputArea


public void buildOutput( int array[][] )
{
// loop through array's rows
for ( int row = 0; row < array.length; row++ ) {

// loop through columns of current row


for ( int column = 0; column < array[ row ].length; column++ )
outputArea.append( array[ row ][ column ] + " " );

outputArea.append( "\n" );
}
}
}

Java Week 04: Arrays

You might also like