examples of arrays in java

1

Objectives of these examples:

  • Declaring an array of primitive data types
  • Using arrays elements, printing arrays values
  • Performing calculations on the array elements
  • Entering the array elements by the user
  • Using for loop statement to manipulate array elements
  • Using the enhanced for Statement to iterate through the array

All Contributions

Write the following method that tests whether the array has four consecutive characters with the same value.

public static boolean isConsecutiveFour(char[] values)

Write a test program that prompts the user to enter a series of characters and displays if the series contains four consecutive characters with the same value. Your program should first prompt the user to enter the input size—i.e., the character of values in the series. 

 

code:

public class Exercise3 {
 public static void main(String[] args) {
Scanner input = new Scanner(System.in); 

 // Prompt the user to enter the input size
 System.out.print("Enter the number of values: ");
 int size=input.nextInt();
 char[] values = new char[size];
 // Promt the user to enter the values
 System.out.print("Enter the values: ");
 for (int i = 0; i < values.length; i++)
 values[i] = input.next().charAt(0);

 // Display if the series contains four consecutive numbers
 System.out.println("The list has" +(isConsecutiveFour(values) ? " " : " no ") +"consecutive fours");
 }

public static boolean isConsecutiveFour(char[] values) {
 int count = 1; // number of consecutive numbers
 for (int i = 0; i < values.length - 1; i++) {
 if (values[i] == values[i + 1])
 count++;
 if (count == 4)
 return true;
 if (values[i] != values[i + 1])
 count = 1;
 }
 return false;
}
} 

 

Write a java program that receives 6 integers from the keyboard, finds the largest number, and counts the occurrence of the largest number entered from the keyboard. Suppose you entered 3, 5, 2, 5, 5, and 5, the largest number is 5 and its occurrence count is 4.

 

sample output:

The array is 3 5 2 5 5 5

The largest number is 5

The occurrence count of the largest number is 4 

---------------

 

answer:

import java.util.Scanner;
public class Exercise2
{
 public static void main(String[] args) {

 final int TOTAL_NUMBERS = 6;
 Scanner input = new Scanner(System.in);
 int[] numbers = new int[TOTAL_NUMBERS];
// Enter the numbers
 for (int i = 0; i < numbers.length; i++) {
System.out.println("Enter a number:");
 numbers[i] = input.nextInt();
 }
 int max = numbers[0];
 for (int i = 1; i < numbers.length; i++) {
 if (max < numbers[i])
 max = numbers[i];
 }
 // Find the occurrence of the largest number
 int count = 0;
 for (int i = 0; i < numbers.length; i++) {
 if (numbers[i] == max) count++;
 }
 // Prepare the result
System.out.println("The array is ");
 for (int i = 0; i < numbers.length; i++) {
 System.out.print(numbers[i] + " ");
 }
System.out.println ("\nThe largest number is " + max);
System.out.println ("\nThe occurrence count of the largest number "
 + "is " + count);
 }
} 

Write a Java program to reverse the elements of an array containing integers.

 

code:

import java.util.Scanner;
public class Exercise1
{
 public static void main(String[] args) {

 int size;

 Scanner console = new Scanner(System.in);
 System.out.print("Enter the size of the array: ");
 size = console.nextInt();
 int[] list = new int[size];
 System.out.println("Enter " + list.length + " integers.");
 for (int i = 0; i < list.length; i++)
 {
 list[i] = console.nextInt();
 }
 int temp;
 // Reversing elements of the array.
 for (int i = 0; i < list.length / 2; i++)
 {
 temp = list[i];
 list[i] = list[list.length - i - 1];
 list[list.length - i - 1] = temp;
 }
System.out.println("Reverse array: ");
 // Display the reverse array.
 for (int i = 0; i < list.length; i++)
 {
 System.out.println(list[i]);
 }
 }
}

 

another way to reverse the array:

import java.util.Scanner;
public class Main
{
 public static void main(String[] args) {

 int size;

 Scanner console = new Scanner(System.in);
 System.out.print("Enter the size of the array: ");
 size = console.nextInt();
 int[] list = new int[size];
 System.out.println("Enter " + list.length + " integers.");
 for (int i = 0; i < list.length; i++)
 {
 list[i] = console.nextInt();
 }
 int temp[]=new int[list.length];
 // Reversing elements of the array.
 int t=temp.length-1;
 for (int i = 0; i < list.length; i++)
 {
     temp[t]=list[i];
     t--;
 }
 list=temp;
System.out.println("Reverse array: ");
 // Display the reverse array.
 for (int i = 0; i < list.length; i++)
 {
 System.out.println(list[i]);
 }
 }
}

Write a java program that reads 7 temperatures from the user and shows which are above and which are below the average of the 7 temperatures.

-------------------

answer:

import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	/* define and create double array a of size 7*/
double ar[]=new double[7];	
//Read temperatures and compute their average
System.out.println("enter 7 tempretures:");
Scanner input=new Scanner(System.in);
double totalTempretures=0;
for(int i=0;i<7;i++)
{
ar[i]=input.nextDouble();
totalTempretures=totalTempretures+ar[i];
}
//compute their average
double average=totalTempretures/7;
System.out.println("the average tempreture is "+average);

   //Display each temperature and its relation to the average
   for(int i=0;i<7;i++)
   {
       System.out.print(ar[i]);
   if (ar[i]>=average) 
   {
       System.out.println(" above average");
        
   }
   else {System.out.println(" below average");}
	}

	}
}

write a java code to accomplish each of the following tasks:

  • declare and create an array named a that is composed of 5 double elements.
  • initialize the values of array a according to the following : 1.7, 2.3, 10.0, 3.5, 2.5
  • display the value of element 3 of array a.
  • initialize each of the 3 first elements of array a to 5.0
  • calculate the summation of the 5 elements of array a then display it.
  • declare and create an array b that is composed by 7 double elements.
  • copy the elements of array a into the first portion of array b.
  • print the elements of array b.

------------------------------

answer:

import java.util.Scanner;
public class HelloWorld
{
	public static void main(String[] args) {
	double a[]={1.7,2.3,10.0,3.5,2.5};
	System.out.println(a[3]);
	a[0]=5.0;
	a[1]=5.0;
	a[2]=5.0;
	System.out.println(a[0]+a[1]+a[2]+a[3]+a[4]);
	double []b= new double[7];
	b[0]=a[0];
	b[1]=a[1];
	b[2]=a[2];
	b[3]=a[3];
	b[4]=a[4];
	for(int i=0;i<b.length;i++)
	{
	System.out.println(b[i]);
	}
	}
}

total contributions (11)

New to examplegeek.com?

Join us