Last active
May 31, 2022 18:52
-
-
Save JohnMeyerhoff/f75f1424513aea1def600fe8c7992e23 to your computer and use it in GitHub Desktop.
Reissverschlussverfahren mit zwei Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Flipswitch { | |
public static void main(String[] args) { | |
int[] rechts = { 1, 2, 3, 4, 5, 6 }; | |
int[] links = { 147, 99, 31, 231, 142, 412, 421, 421, 241 }; | |
int[] zip = flipswitch(links, rechts); | |
System.out.println(Arrays.toString(links)); | |
System.out.println(Arrays.toString(rechts)); | |
System.out.println("Reissverschluss:"); | |
System.out.println(Arrays.toString(zip)); | |
} | |
public static int[] flipswitch(int[] links, int[] rechts) { | |
int[] out = new int[(links.length + rechts.length)]; | |
boolean rechtskurz = (rechts.length < links.length); | |
// reißverschluss | |
int[] kurz = rechtskurz ? rechts : links; | |
int[] lang = rechtskurz ? links : rechts; | |
int a = 0; | |
int b = 0; | |
boolean flip = true; | |
for (int i = 0; i < out.length; i++) { | |
if (flip && a < kurz.length) {// a lesen | |
out[i] = kurz[a++]; | |
} else { | |
out[i] = lang[b++]; // schreiben stelle b nach i | |
} | |
flip = !flip; | |
} | |
return out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice