ArrayList and Itrator #JAVA code
Iterator it = arl.iterator();
while(it.hasNext())
System.out.println("Iterator in action: " + it.next());
Iterator it = arl.iterator();
while(it.hasNext())
System.out.println("Iterator in action: " + it.next());
6
1 2 3 4 10 11
31
2
0 2 10
5 3 5
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
4
1 4 3 2
2 3 4 1
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int arr[] = new int[n]; for(int arr_i=0; arr_i < n; arr_i++){ arr[arr_i] = in.nextInt(); } for(int i=(arr.length-1); i>=0; i--) { System.out.print(arr[i]+" "); } } }
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
a b c
d
e f g
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
19
1 1 1 1 1 0 1 0 0 0 0 0
1 0 0 0
1 1 1 1 1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0
1 1 0 0
0 0 2 0 2 4 2 4 4 4 4 0
1 1 1 1 1 0 1 0 0 0 0 0
0 2 4 4
0 0 0 0 0 2 0 2 0 2 0 0
0 0 2 0 2 4 2 4 4 4 4 0
0 0 2 0
0 0 1 0 1 2 1 2 4 2 4 0
2 4 4
2
1 2 4
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Scanner;
public class twoDArray {
public static void main(String[] args) {
/* Create an input Scanner instance for reading input */
Scanner stdIn = new Scanner(System.in);
/* Name some constants used in the program */
int rows = 6; // Number of rows in the matrix
int cols = 6; // Number of columns in the matrix
int minValueInArray = -9; // Minimum value of an element in the matrix
int elementsInHourGlass = 7;// Number of elements in an hourglass
// Set max to the minimum hourglass sum possible
int maxHourGlassSum = minValueInArray * elementsInHourGlass;
// Declare the matrix of numbers
int matrix[][] = new int[rows][cols];
int hourGlassSum; // Sum of the elements in the hourglass
/* Read the values for the matrix looping thru the rows */
for(int i=0; i < rows; i++){
/* Loop thru the columns for each row, reading the matrix */
for(int j=0; j < cols; j++){
/* Read the next value from standard input */
matrix[i][j] = stdIn.nextInt();
}
}
/* For debugging, print out the matrix */
for(int i=0; i < rows; i++){
for(int j=0; j < cols; j++){
// System.out.print(matrix[i][j] + " ");
}
// System.out.println();
}
/* Loop thru the possible starting points of the hourglass */
for (int i=0; i < (rows-2); i++) {
for (int j=0; j<(cols-2); j++) {
/* Print the hourglass starting position */
//System.out.println ("Checking hourglass at (" + i + "," + j + ")");
/* Print out the hourglass elements */
//System.out.println(matrix[i][j] + " " + matrix[i][j+1] + " " + matrix[i][j+2]);
// System.out.println(" " + matrix[i+1][j+1]);
// System.out.println(matrix[i+2][j] + " " + matrix[i+2][j+1] + " " + matrix[i+2][j+2]);
/* Compute the sum of the elements in the hourglass */
hourGlassSum = matrix[i][j] + matrix[i][j+1] + matrix[i][j+2] +
matrix[i+1][j+1] +
matrix[i+2][j] + matrix[i+2][j+1] + matrix[i+2][j+2];
/* Print out the sum of the elements in this hourglass */
//System.out.println("hour glass sum = " + hourGlassSum);
// System.out.println("max glass sum = " + maxHourGlassSum );
/* Is the new hourglass sum greater than the max found so far */
if (hourGlassSum > maxHourGlassSum) {
/* If so, then replace the max hour glass sum, with the current sum */
maxHourGlassSum = hourGlassSum;
}
}
}
/* Print out the maximum hour glass sum */
System.out.println(maxHourGlassSum);
}
}