public class SpiralMatrix { public static void main(String[] args) { int[][] matrix = new int[7][7]; int top = 0; int bottom = matrix.length - 1; int left = 0; int right = matrix[0].length - 1; int index = 1; while (left <= right && top <= bottom) { for (int col = left; col <= right; col++) { matrix[top][col] = index++; } top++; for (int row = top; row <= bottom; row++) { matrix[row][right] = index++; } right--; for (int col = right; col >= left ; col--) { matrix[bottom][col] = index++; } bottom--; for (int row = bottom; row >= top ; row--) { matrix[row][left] = index++; } left++; } for (int row = 0; row < matrix.length; row++) { for (int col = 0; col < matrix[0].length; col++) { System.out.printf("%02d ", matrix[row][col]); } System.out.println(); } // result: // 01 02 03 04 05 06 07 // 24 25 26 27 28 29 08 // 23 40 41 42 43 30 09 // 22 39 48 49 44 31 10 // 21 38 47 46 45 32 11 // 20 37 36 35 34 33 12 // 19 18 17 16 15 14 13 } }
Tag: java
How to convert letter char to its alphabet integer position in Java
public class LetterToAlphabetPos { public static void main(String[] args) { char letter = 'Z'; System.out.println(letterToAlphabetPos(letter)); // returns 26 } private static int letterToAlphabetPos(char letter) { return Character.toUpperCase(letter) - 64; } }
How to generate Eratosthenes sieve in Java
public class EratosthenesSieve { public static final int MAX = 121; public static void main(String[] args) { boolean[] primes = new boolean[MAX]; eratosthenesSieve(primes); System.out.printf("Primes in range [2..%d] are: ", MAX); for (int i = 2; i < MAX; i++) { if(primes[i]) { System.out.printf(i + " "); } } } private static void eratosthenesSieve(boolean[] primes) { for (int i = 2; i < primes.length; i++) { primes[i] = true; } for (int i = 2; i < Math.sqrt(MAX); i++) { if (primes[i]) { for (int j = i * i; j < MAX; j += i) { primes[j] = false; } } } } }
How to find avearge in Collection of integers in Java using Stream API
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class FindAverage { public static void main(String[] args) { List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 5, 6, 18, 22, 33, 14, 0)); double listAvg = nums.stream() .mapToDouble(num -> num) .average() .getAsDouble(); System.out.println(listAvg); // will return 11.222222222222221 } }
How to print list separated by comma in Java
import java.util.ArrayList; import java.util.stream.Collectors; public class CommaDelimeter { public static void main(String[] args) { ArrayList<Integer> collectNums = new ArrayList<Integer>(); collectNums.add(2); collectNums.add(9); collectNums.add(12); String joined = collectNums.stream() .map(Object::toString) .collect(Collectors.joining(", ")); System.out.println(joined); } }
How to generate random integer in range in Java
import java.util.Random; public class RandomNumInRange { public static void main(String[] args) { int min = 10; int max = 20; Random rand = new Random(); int randNum = rand.nextInt((max - min) + 1) + min; System.out.print(randNum); } }
How to split by forward or/and back slash in Java using RegEx
public class MatchBackslashRegex { public static void main(String[] args) { String string = "Element1\\Element2"; String[] strArr = string.split("/\\\\"); System.out.println(strArr[0]); // returns Element1 } }
How to reverse String in Java
public class ReverseString { public static void main(String[] args) { String a = "Test"; String aReversed = new StringBuilder(a).reverse().toString(); System.out.println(aReversed); // returns tseT } }