private static void buildSquare() {
int i = 0;
for(Character elem: line) {
LinkedList<Character> tmp = new LinkedList<>(line);
Collections.rotate(tmp, i++); // i инкрементируется сразу в месте использования. (Попробуйте i--, так, кажется, будет правильнее)
square.put(elem, tmp);
}
}
private void getSuit() {
int randomSuit = new Random().nextInt(suit.length);
System.out.print(suit[randomSuit]);
}
private static String myFunction(String url) {
if (some condition) {
System.err.println(url + " " + statusCode); // Видимо, логирование в консоль
return url + " " + statusCode + "\n"; // Возврат результата
}
return ""; // Это же пример, верно?))
}
public static void main(String[] args) throws IOException {
FileWriter err = new FileWriter("error.txt");
String result = myFunction(args);
er.write(result);
//some logic
err.write(something);
err.close();
}
import java.util.List;
import java.util.Random;
import java.util.function.Function;
class RandomFunctor {
private final List<Function<Integer, String>> functionList = List.of(
integer -> {
return "this is from first " + integer; //TODO :: implement me!
},
integer -> {
return "this is from second " + integer; //TODO :: implement me!
},
integer -> {
return "this is from third " + integer; //TODO :: implement me!
});
public String getRandomString(Integer input) {
var rand = new Random();
var function = functionList.get(rand.nextInt(functionMap.size()));
return function.apply(input);
}
}
public static void main(String args[]) {
LinkedList<Integer> linkedList = new LinkedList();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);
linkedList.add(5);
linkedList.add(6);
//finding middle element of LinkedList in single pass
Iterator<Integer> current = linkedList.iterator();
int length = 0;
Iterator<Integer> middle = linkedList.iterator();
Integer result = 0;
while (current.hasNext()) {
length = current.next();
if (length % 2 == 0) {
result = middle.next();
}
}
if (length % 2 == 1) {
result = middle.next();
}
System.out.println("length of LinkedList: " + length);
System.out.println("middle element of LinkedList : " + result);
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
enum TestEnum {
One(1), Two(2), Three(3);
private int code;
TestEnum(int code) {
this.code = code;
}
}
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
boolean isPresent = Arrays.stream(TestEnum.values()).anyMatch(element -> element.code == n);
System.out.println("n in TestEnum? " + isPresent);
}
}