Введение в программирование
Массивы и ссылки
int[] ints; double[] doubles; boolean[] booleans; String[] strings;
ints = new int[10]; doubles = new double[20]; booleans = new boolean[30]; strings = new String[40];
System.out.println(ints.length); // 10 System.out.println(doubles.length); // 20 System.out.println(booleans.length); // 30 System.out.println(strings.length); // 40
System.out.println(ints[0]); // 0 System.out.println(doubles[1]); // 0.0 System.out.println(booleans[2]); // false System.out.println(strings[3]); // null
ints = new int[]{1, 2, 3}; doubles = new double[]{1}; booleans = new boolean[]{true, false}; strings = new String[]{"a", "b", "c"};
// i – index for (int i = 0; i < ints.length; i++) { System.out.println(ints[i]); }
// i – int for (int i : ints) { System.out.println(i); }
int[][] ints2d; int[][][] ints3d;
ints2d = new int[10][20]; ints3d = new int[10][20][30]; System.out.println(ints2d[0]); // [I... System.out.println(ints3d[0]); // [[I...
ints2d = new int[10][]; ints3d = new int[10][][]; System.out.println(ints2d[0]); // null System.out.println(ints3d[0]); // null
for (int i = 0; i < ints2d.length; i++) { ints2d[i] = new int[i]; }
for (int[] row : ints2d) { System.out.println(row.length); }
int[][] ints2d = new int[][]{{1, 2, 3}, {4, 5, 6}}; System.out.println(ints2d.length); // 2 System.out.println(ints2d[0].length); // 3 System.out.println(ints2d[1].length); // 3
int[][] ints2d = new int[][]{{1}, {2, 3}, null}; System.out.println(ints2d.length); // 3 System.out.println(ints2d[0].length); // 1 System.out.println(ints2d[1].length); // 2 System.out.println(ints2d[2]); // null
int[] as = new int[10]; int[] bs = as; as[1] = 10; System.out.println(as[1] + " " + bs[1]); // 10 10 System.out.println(as == bs); // true
bs = as; as = new int[10]; as[1] = 20; System.out.println(as[1] + " " + bs[1]); // 20 10 System.out.println(as == bs); // false
void fill(int[] ints, int value) { for (int i = 0; i < ints.length; i++) { ints[i] = value; } }
fill(as, 100); // 100 100 100 System.out.println(as[0] + " " + as[1] + " " + as[2]);
void referenceAsValue(int[] ints) { ints = new int[0]; }
bs = as; referenceAsValue(as); System.out.println(as == bs); // true
int[] create(int length, int value) { int[] ints = new int[length]; fill(ints, value); return ints; }
as = create(3, 123); System.out.println(as[0] + " " + as[1] + " " + as[2]); // 123 123 123
for (int i = 0; i < 10_000_000; i++) { as = new int[1000_000]; } System.out.println("ok");
as = new int[]{0, 10, 20, 30, 40, 50}; bs = create(5, -1); System.arraycopy(as, 2, bs, 1, 3); // bs = {-1, 20, 30, 40, -1}
int[] as = new int[]{0, 10, 20, 30}; System.out.println(as); // [I@3834d63f System.out.println(java.util.Arrays.toString(as)); // [0, 10, 20, 30] // import java.util.Arrays; System.out.println(Arrays.toString(as)); // [0, 10, 20, 30]
// Стандартный ввод (stdin) Scanner scanner = new Scanner(System.in); System.out.print("Input two numbers: "); int a = scanner.nextInt(); int b = scanner.nextInt(); System.out.println("Sum of numbers: " + (a + b));