Question
I need help with some java code here. There is output of what it should look like below.
Exercise Generic: Write a generic method that is called printArrayReversed It has one parameter – an array of type T – and it returns nothing It prints all the elements of the array separated by a single space (blank) and in reverse order When all elements have been printed the method printArrayReversed advances the cursor to the next line Test the method with at least 2 different arrays and print the results
Output:
fly dog cat bat
u o i e a
Explanation / Answer
using namespace System; void PrintIndexAndValues( Array^ myArray ); void main() { // Creates and initializes a new Array instance. Array^ myArray = Array::CreateInstance( String::typeid, 5 ); myArray->SetValue( "a", 0 ); myArray->SetValue( "e", 1 ); myArray->SetValue( "i", 2 ); myArray->SetValue( "o", 3 ); myArray->SetValue( "u", 4 ); // Displays the values of the Array. Console::WriteLine( "The Array instance initially contains the following values:" ); PrintIndexAndValues( myArray ); // Reverses the sort of the values of the Array. Array::Reverse( myArray ); // Displays the values of the Array. Console::WriteLine( "After reversing:" ); PrintIndexAndValues( myArray ); } void PrintIndexAndValues( Array^ myArray ) { for ( int i = myArray->GetLowerBound( 0 ); i GetUpperBound( 0 ); i++ ) Console::WriteLine( " [{0}]: {1}", i, myArray->GetValue( i ) ); } /* This code produces the following output. The Array instance initially contains the following values: [0]: a [1]: e [2]: i [3]: o [4]: u After reversing: [0]: u [1]: o [2]: i [3]: e [4]: a