Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class processingArrays {
- public static void main(String[] args) {
- // processing arrays with specific numbers like evens or 1-9
- int[] numbers = new int[9];
- for (int i=0; i< numbers.length; i++){
- numbers[i] = i+1;
- }
- // processing arrays with user input
- Scanner kb = new Scanner(System.in);
- String[] stringArray = new String[5];
- System.out.println("processing arrays with user input");
- for (int i=0; i< stringArray.length; i++){
- System.out.println("Enter your array value");
- stringArray[i]= kb.nextLine();
- }
- System.out.println("Testing \n" + stringArray[2]);
- System.out.println("printed the value at stringArray[2]");
- System.out.println();
- // initializing an array with random values between 50 and 100
- int [] random = new int[12];
- for (int i=0; i<random.length; i++)
- random[i] = 50 + (int)(Math.random( )* 51);
- System.out.println();
- // printing arrays. One way with the for loop. Print array "numbers"
- System.out.println("printing arrays. One way with the for loop. Print array \"numbers\"");
- for (int i =0; i<numbers.length; i++)
- System.out.print(numbers[i] + " ");
- System.out.println();// goto next line
- System.out.println();
- System.out.println("Printing array \"stringArray\" using for loop");
- for (int i = 0; i<stringArray.length; i++)
- System.out.print(stringArray[i]+ " ");
- System.out.println(); // goto next line
- System.out.println();
- // printing arrays using a for each loop
- System.out.println("printing arrays using a for each loop");
- for (int i : random)
- System.out.print(i+" ");
- // goto next line
- System.out.println();
- System.out.println();
- // summing all the elements
- System.out.println("the sum of all the elements of the array");
- double[] decimal = new double[8];
- double sum=0;
- for (int i=0; i<decimal.length; i++){
- decimal[i]= (Math.random() * 10 );
- decimal[i]= ((int)(decimal[i] * 100))/100.0; // this is a neat way to round
- sum = sum + decimal[i];
- }
- for (double e : decimal){
- System.out.print(e+" ");
- }
- System.out.println();
- System.out.println(sum);
- System.out.println();
- // finding the largest element
- System.out.println("the largest element of the array");
- int[] elements = new int[9];
- int indexOfLargestElement=0;
- int largestElement=elements[0];
- for (int i=0; i<elements.length; i++){
- elements[i] = (int)(Math.random()* 31);
- }
- for (int i =1; i< elements.length-1; i++){
- if (elements[indexOfLargestElement]< elements[i]){
- indexOfLargestElement = i;
- largestElement=elements[i];
- }
- }
- for (int e : elements)
- System.out.print(e+ " ");
- System.out.println();
- System.out.println(elements[indexOfLargestElement]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement