An array of type int
(3.a )
// 2016-11-16 //
// uses my custom class TakeInLine
// enters integer data, finds sum, average, sorts, finds value, erase, show ... //
import java.util.Arrays;
class ArrayInt {
final static int MAX_SIZE = 5; // use a small size here to ease testing //
public static void main( String[] args ) {
int[] myAry = new int[ MAX_SIZE ]; // create to hold up to MAX_SIZE int's //
int size = 0;
TakeInLine til = new TakeInLine();
do {
myAry[ size ] = til.takeInInt( "Enter next integer to sum: " );
++ size;
if( size == MAX_SIZE ) {
System.out.println( "You have reached the maximum "
+ MAX_SIZE + " number of elements." );
break;
}
} while( til.more() );
double sum = sumAry( myAry, size );
System.out.println( "\nFor " + size+ " numbers entered, sum is "
+ sum + " and array average is " + sum/size );
System.out.print( "\nshowAry: " );
showAry( myAry, size );
int last = myAry[ size-1 ]; // size is always at least 1 //
Arrays.sort( myAry, 0, size );
reverse( myAry, size );
System.out.print( "\nAfter sorting and reversing ...\nshowAry: " );
showAry( myAry, size );
int i = findIndex( myAry, size, last );
if( i != -1 ) {
size = eraseIndex( myAry, size, i );
System.out.println( "\nAfter erasing " + last + " ..." );
System.out.println( "\nshowAry: " );
showAry( myAry, size );
} else {
System.out.println( last + " NOT found in array." );
}
}
static double sumAry( final int[] ary, final int size ) {
double sum = 0.0;
for( int i = 0; i < size; ++ i ) {
sum += ary[i];
}
return sum;
}
static void showAry( final int[] ary, final int size ) {
for( int i = 0; i < size; ++ i ) {
System.out.print( ary[i] + " " );
}
System.out.println();
}
static int findIndex( final int[] ary, final int size, final int value ) {
for( int i = 0; i < size; ++i ) {
if( ary[i] == value )
return i;
}
// else if reach here ...
return -1;
}
static int eraseIndex( int[] ary, final int size,
final int index ) {
for( int i = index; i < size-1; ++i ) {
ary[i] = ary[i+1]; // copy each element above, down one index, so erased
}
return size-1;
}
static void reverse( int[] ary, final int size ) {
int tmp;
for( int i = 0, end = size - 1; i < end; ++i, --end ) {
tmp = ary[i];
ary[i] = ary[end];
ary[end] = tmp;
}
}
}
(3.b) <-- a variant of (3.a)
// 2016-11-16 //
// uses my custom class TakeInLine
// enters integer data, finds sum, average, sorts, finds value, erase, show ... //
import java.util.Arrays;
import java.util.stream.IntStream;
class ArrayInt2 {
final static int MAX_SIZE = 5; // use a small size here to ease testing //
public static void main( String[] args ) {
int[] myAry = new int[ MAX_SIZE ]; // create ALL zero, to hold up to MAX_SIZE int's //
int size = 0;
TakeInLine til = new TakeInLine();
do {
myAry[ size ] = til.takeInInt( "Enter next integer to sum: " );
++ size;
if( size == MAX_SIZE ) {
System.out.println( "You have reached the maximum "
+ MAX_SIZE + " number of elements." );
break;
}
} while( til.more() );
//double sum = sumAry( myAry, size );
//double sum = IntStream.of( myAry ).sum();
double sum = IntStream.of( myAry ).parallel().sum(); // NOTE! Any unused elements were initialed to 0 value //
System.out.println( "\nFor " + size+ " numbers entered, sum is "
+ sum + " and array average is " + sum/size );
System.out.print( "\nshowAry: " );
showAry( myAry, size );
int last = myAry[ size-1 ]; // size is always at least 1 //
Arrays.sort( myAry, 0, size );
System.out.print( "\nAfter sorting ...\nshowAry: " );
showAry( myAry, size );
//int i = findIndex( myAry, size, last );
int i = Arrays.binarySearch( myAry, 0, size, last );
if( i != -1 ) {
size = eraseIndex( myAry, size, i );
System.out.print( "\nAfter erasing " + last + " and reversing ... " );
reverse( myAry, size );
System.out.println( "\nshowAry: " );
showAry( myAry, size );
} else {
System.out.println( last + " NOT found in array." );
}
}
/*
static double sumAry( final int[] ary, final int size ) {
double sum = 0.0;
for( int i = 0; i < size; ++ i ) {
sum += ary[i];
}
return sum;
}
*/
static void showAry( final int[] ary, final int size ) {
for( int i = 0; i < size; ++ i ) {
System.out.print( ary[i] + " " );
}
System.out.println();
}
/*
static int findIndex( final int[] ary, final int size, final int value ) {
for( int i = 0; i < size; ++i ) {
if( ary[i] == value )
return i;
}
// else if reach here ...
return -1;
}
*/
static int eraseIndex( int[] ary, final int size, final int index ) {
for( int i = index; i < size-1; ++i ) {
ary[i] = ary[i+1]; // copy each element above, down one index, so erased
}
return size-1;
}
static void reverse( int[] ary, final int size ) {
int tmp;
for( int i = 0, end = size - 1; i < end; ++i, --end ) {
tmp = ary[i];
ary[i] = ary[end];
ary[end] = tmp;
}
}
}
And also, an array of Java type Integer
(3.c)
/// 2016-11-16 //
// uses my custom class TakeInLine
// enters integer data, finds sum, average, sorts, finds value, erase, show ... //
import java.util.Arrays;
import java.util.Comparator;
class ArrayInteger
{
final static int MAX_SIZE = 5; // use a small size here to ease testing //
public static void main( String[] args ) {
// create to hold up to MAX_SIZE int's //
Integer[] myAry = new Integer[ MAX_SIZE ];
int size = 0; // index of next postion available to fill //
TakeInLine til = new TakeInLine();
do {
myAry[ size ] = til.takeInInt( "Enter next integer to sum: " );
++ size;
if( size == MAX_SIZE ) {
System.out.println( "You have reached the maximum "
+ MAX_SIZE + " number of elements." );
break;
}
} while( til.more() );
double sum = sumAry( myAry, size );;
System.out.println( "\nFor " + size+ " numbers entered, sum is "
+ sum + " and array average is " + sum/size );
System.out.print( "\nshowAry: " );
showAry( myAry, size );
Integer last = myAry[ size-1 ]; // size is always at least 1 //
Arrays.sort( myAry, 0, size );
System.out.print( "\nAfter sorting ...\nshowAry: " );
showAry( myAry, size );
//int i = findIndex( myAry, size, last );
int i = Arrays.binarySearch( myAry, 0, size, last );
if( i != -1 ) {
size = eraseIndex( myAry, size, i );
System.out.println( "\nAfter erasing " + last + " ..." );
Arrays.sort( myAry, 0, size, new ReverseIntegers() );
System.out.print( "\nAfter sorting in reversed order ...\nshowAry: " );
showAry( myAry, size );
} else {
System.out.println( last + " NOT found in array." );
}
}
static double sumAry( final Integer[] ary, final int size ) {
double sum = 0.0;
for( int i = 0; i < size; ++ i ) {
sum += ary[i];
}
return sum;
}
static void showAry( final Integer[] ary, final int size ) {
for( int i = 0; i < size; ++ i ) {
System.out.print( ary[i] + " " );
}
System.out.println();
}
/*
static int findIndex( final Integer[] ary, final int size,
final Integer value ) {
for( int i = 0; i < size; ++i ) {
if( value == ary[i] ) {
return i;
}
}
// else if reach here ...
return -1;
}
*/
static int eraseIndex( Integer[] ary, final int size, final int index ) {
for( int i = index; i < size-1; ++i ) {
ary[i] = ary[i+1]; // copy each element above, down one index, so erased
}
return size-1;
}
}
class ReverseIntegers implements Comparator < Integer > {
public int compare( Integer a, Integer b ) {
return b.compareTo(a);
}
}