Commit 6f621092 by Patryk Czarnik

Dalsze przykłady na tablice

parent 3d005279
...@@ -62,7 +62,7 @@ public class C_Petle { ...@@ -62,7 +62,7 @@ public class C_Petle {
} }
System.out.println(Arrays.toString(liczby)); System.out.println(Arrays.toString(liczby));
// Zwykłe pętli for używamy też wtedy, gdy algorytm wymaga jednoczesnego dostępu do różnych elementów. // Zwykłej pętli for używamy też wtedy, gdy algorytm wymaga jednoczesnego dostępu do różnych elementów.
} }
} }
package p11_tablice;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class FunkcjeNaTablicach {
static Integer max(int[] t) {
if(t.length == 0) {
return null;
}
int max = Integer.MIN_VALUE;
for (int e : t) {
if (e > max) {
max = e;
}
}
return max;
}
static Integer min(int[] t) {
if(t.length == 0) {
return null;
}
int min = Integer.MAX_VALUE;
for (int e : t) {
if (e < min) {
min = e;
}
}
return min;
}
static Integer min_v2(int[] t) {
if(t.length == 0) {
return null;
}
int min = t[0];
for (int i = 1; i < t.length; i++) {
if (t[i] < min) {
min = t[i];
}
}
return min;
}
static int roznicaMinMax_v1(int[] t) {
// wersja "lepsza" jeśli kryterium oceny jest działanie, wydajność - bo tylko jedno przejście przez dane
if(t.length == 0) {
return 0;
}
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int e : t) {
if (e < min) {
min = e;
}
if (e > max) {
max = e;
}
}
return max - min;
}
static int roznicaMinMax_v2(int[] t) {
// wersja "lepsza" jeśli kryterium oceny jest styl, struktura kodu - bo korzystamy z już wcześniej napisanych rzeczy
return t.length == 0 ? 0 : max(t) - min(t);
}
static void wypiszWieksze(int[] t, int x) {
for (int e : t) {
if (e > x) {
System.out.print(e + ", ");
}
}
System.out.println();
}
static Integer pierwszaWieksza(int[] t, int x) {
for (int e : t) {
if (e > x) {
return e;
}
}
return null;
}
static int sumaWiekszych(int[] t, int x) {
int suma = 0;
for (int e : t) {
if (e > x) {
suma += e;
}
}
return suma;
}
static int ileWiekszych(int[] t, int x) {
int ile = 0;
for (int e : t) {
if (e > x) {
ile += 1;
}
}
return ile;
}
static void wypiszPodzielne(int[] t, int x) {
for (int e : t) {
if (e % x == 0) {
System.out.print(e + "; ");
}
}
System.out.println();
}
static Integer pierwszaPodzielna(int[] t, int x) {
for (int e : t) {
if (e % x == 0) {
return e;
}
}
return null;
}
static int sumaPodzielnych(int[] t, int x) {
int suma = 0;
for (int e : t) {
if (e % x == 0) {
suma += e;
}
}
return suma;
}
static int ilePodzielnych(int[] t, int x) {
int ile = 0;
for (int e : t) {
if (e % x == 0) {
ile++;
}
}
return ile;
}
static Integer znajdzWspolny(int[] t1, int[] t2) {
for (int e1 : t1) {
for (int e2 : t2) {
if (e1 == e2) {
return e1;
}
}
}
return null;
}
static List<Integer> wszystkieWspolne(int[] t1, int[] t2) {
List<Integer> lista = new ArrayList<>();
for (int e1 : t1) {
for (int e2 : t2) {
if (e1 == e2) {
lista.add(e1);
}
}
}
return lista;
}
}
package p11_tablice;
import java.util.Arrays;
public class Kopiowanie1 {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6,7,8,9};
// clone(), copyOf(), copyOfRange() - tworzą nową tablicę wypełnioną takimi samymi danymi (lub fragment)
int[] b = a.clone();
int[] c = Arrays.copyOf(a, 5);
int[] d = Arrays.copyOf(a, 12);
int[] e = Arrays.copyOfRange(a, 4, 8);
int[] f = Arrays.copyOfRange(a, 4, 12);
// arraycopy: Docelowa tablica musi istnieć
int[] g = new int[8];
System.arraycopy(a, 2, g, 1, 5);
// Referencja do tej samej tablicy (do tego samego obiektu)
// Zmiany w tablicy a będą widziane przez zmienną r i vice versa
int[] r = a;
System.out.println("a: " + Arrays.toString(a));
System.out.println("b: " + Arrays.toString(b));
System.out.println("c: " + Arrays.toString(c));
System.out.println("d: " + Arrays.toString(d));
System.out.println("e: " + Arrays.toString(e));
System.out.println("f: " + Arrays.toString(f));
System.out.println("g: " + Arrays.toString(g));
System.out.println("r: " + Arrays.toString(r));
System.out.println();
System.out.println("Zmieniam a w środku");
a[0]=13;
a[1]++;
a[2]+=10;
a[3]*=5;
System.out.println("a: " + Arrays.toString(a));
System.out.println("b: " + Arrays.toString(b));
System.out.println("c: " + Arrays.toString(c));
System.out.println("d: " + Arrays.toString(d));
System.out.println("e: " + Arrays.toString(e));
System.out.println("f: " + Arrays.toString(f));
System.out.println("g: " + Arrays.toString(g));
System.out.println("r: " + Arrays.toString(r));
System.out.println();
System.out.println("Na a wpisuję nową tablicę");
a = new int[] {77,88,99};
System.out.println("a: " + Arrays.toString(a));
System.out.println("b: " + Arrays.toString(b));
System.out.println("r: " + Arrays.toString(r));
}
}
package p11_tablice;
import java.util.Arrays;
public class Kopiowanie2 {
public static void main(String[] args) {
int[][] a = {{1,2,3,4,5}, {10,20,30,40}};
int[][] r = a;
int[][] b = a.clone();
System.out.println(Arrays.deepToString(a));
System.out.println(Arrays.deepToString(r));
System.out.println(Arrays.deepToString(b));
System.out.println();
a[1][1] = 77; // zmienia się w a i b
System.out.println(Arrays.deepToString(a));
System.out.println(Arrays.deepToString(r));
System.out.println(Arrays.deepToString(b));
System.out.println();
a[0] = new int[] {300, 400, 500}; // zmienia się tylko w a (i r)
System.out.println(Arrays.deepToString(a));
System.out.println(Arrays.deepToString(r));
System.out.println(Arrays.deepToString(b));
System.out.println();
}
}
package p11_tablice;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
public class Kopiowanie3 {
public static void main(String[] args) {
AtomicInteger[] t = new AtomicInteger[4];
t[0] = new AtomicInteger(5);
t[1] = new AtomicInteger(10);
AtomicInteger[] kopia = t.clone();
System.out.println(Arrays.toString(t));
System.out.println(Arrays.toString(kopia));
t[0].addAndGet(100); // widać w kopii
t[1] = new AtomicInteger(333); // nie widać w kopii
System.out.println(Arrays.toString(t));
System.out.println(Arrays.toString(kopia));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment