Here is the first class:
import java.io.*; import java.util.*; public class Game { private int srtLoc; private String result; private boolean tri; private ArrayList<Game> Games = new ArrayList<>(); public static void main(String[] args) { Game newGame = new Game(); newGame.setList(1, -1); //ΡΠΎΠ·Π΄Π°ΡΠΌ ΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡΡ Π² 'Games' newGame.removeMe(); //ΡΠ΄Π°Π»ΡΠ΅ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡ ΠΏΠΎ Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡ ΠΈΠ½Π΄Π΅ΠΊΡΡ Game2 literatic = new Game2(); literatic.m1(); //ΡΠΎΠ·Π΄Π°ΡΠΌ ΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡΡ Π² 'MyFavourite' favourite(); //Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠ²Π΅Π΄Π΅Π½ΠΈΡ ΠΎ ΠΌΠΎΠΈΡ
Π»ΡΠ±ΠΈΠΌΡΡ
ΠΈΠ³ΡΠ°Ρ
} private String setList (int a, int b) { if (a == 1) { Game one = new Game(); Game two = new Game(); Game three = new Game(); Game four = new Game(); Game five = new Game(); Games.add(one); Games.add(two); Games.add(three); Games.add(four); Games.add(five); outPrint(); //ΠΏΠΎΠΊΠ°Π·ΡΠ²Π°Π΅ΠΌ ΠΊΠ°ΠΊΠΎΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ Π² ΡΡΠ΅ΠΉΠΊΡ Ρ ΠΊΠ°ΠΊΠΈΠΌ ΠΈΠ½Π΄Π΅ΠΊΡΠΎΠΌ Π±ΡΠ» Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ System.out.println("Π ArrayList 'Games' ΡΠ΅ΠΉΡΠ°Ρ " + Games.size() + " ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ²(Π°)!"); System.out.println("'Games' ΠΏΡΡΡ? " + Games.isEmpty()); } if (a == 2) { for (Game GoodGame : Games) { int rob = Games.indexOf(GoodGame); if (b == rob) { b++; result = Integer.toString(b); break; } } if (b > Games.size() || b < 0) { System.out.println("Π 'Games' Π½Π΅Ρ ΡΠ°ΠΊΠΈΡ
ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ²"); result = "Π½ΠΈΡΠ΅Π³ΠΎ"; } } return result; } private void removeMe() { String resulting = null; String userInputing = getUserInput("ΠΠ°ΠΊΠΎΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ Π²Ρ Ρ
ΠΎΡΠΈΡΠ΅ ΡΠ΄Π°Π»ΠΈΡΡ?"); if (userInputing != null) { int userInput = Integer.parseInt(userInputing); resulting = checkYourself(userInput); } if (resulting != "Π½ΠΈΡΠ΅Π³ΠΎ") { System.out.println("ΠΡ ΡΠ΄Π°Π»ΡΠ΅ΡΠ΅ " + "'Game " + resulting + "'"); System.out.println("Π ArrayList 'Games' ΡΠ΅ΠΉΡΠ°Ρ " + Games.size() + " ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ²(Π°)!"); } else { System.out.println("ΠΡ " + resulting + " Π½Π΅ ΡΠ΄Π°Π»ΡΠ΅ΡΠ΅."); System.out.println("Π ArrayList 'Games' ΡΠ΅ΠΉΡΠ°Ρ " + Games.size() + " ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ²(Π°)!"); } } private String getUserInput(String prompt) { String inputLine = null; System.out.println(prompt + " "); try { BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); inputLine = is.readLine(); if (inputLine.length() == 0) return null; } catch (IOException e) { System.out.println("IOException:" + e); } return inputLine.toLowerCase(); } private String checkYourself(int userInput1) { for (Game srt : Games) { srtLoc = Games.indexOf(srt); if (srtLoc == userInput1) { tri = true; break; } } result = setList(2, userInput1); if (tri) { Games.remove(srtLoc); } return result; } private void outPrint() { int loc = 1; for (Game wer : Games) { int x = Games.indexOf(wer); System.out.println("Π― Π΄ΠΎΠ±Π°Π²Π»ΡΡ Π² 'Games' ΠΎΠ±ΡΠ΅ΠΊΡ Game " + loc + " ΠΏΠΎ ΠΈΠ½Π΄Π΅ΠΊΡΡ " + x); loc++; } } private static void favourite() { Game2 right = new Game2(); System.out.println("ΠΠΎΠΈ Π»ΡΠ±ΠΈΠΌΡΠ΅ ΠΈΠ³ΡΡ: " + right.getNameToGame2(0) + ", " + right.getNameToGame2(1) + ", " + right.getNameToGame2(2)); } } But the second class:
import java.util.*; public class Game2 extends Game { private String name; private String myName; private ArrayList<Game2> MyFavourite = new ArrayList<>(); public void m1() { Game2 first = new Game2(); Game2 second = new Game2(); Game2 third = new Game2(); MyFavourite.add(first); MyFavourite.add(second); MyFavourite.add(third); first.setName("RimWord"); second.setName("Diablo 2"); third.setName("ΠΠΎΡΠΌΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΡΠ΅ΠΉΠ½Π΄ΠΆΠ΅ΡΡ 2"); } private void setName(String n) { name = n; } private String getName() { return name; } public String getNameToGame2(int h) { for (Game2 j : MyFavourite) { System.out.println("Test"); if (h == MyFavourite.indexOf(j)) { myName = j.getName(); break; } } return myName; } } After calling favourite(); gives my favorite games a list of null, null, null. It does not even output "Test", as I understand it, the JVM in for(Game2 j : MyFavourite) { } does not enter at all, although the rest of the program behaves well (the compiler skips not a word). This code is not particularly important, I made it to get a better look at ArrayList functions, but I ran into such a problem. Help me please! (I program in Java for about a week, the knowledge is tiny, so donβt be angry that the code is straightforward and not exactly OOP, I'm just learning).

Game2 right = new Game2();need to callm1();, and then output to the console. When creating anew Game2()object, thenew Game2()fieldMyFavouriteempty, so null is displayed - MrFylypenko