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).

That's what the JVM gives out in the end.

  • After calling Game2 right = new Game2(); need to call m1(); , and then output to the console. When creating a new Game2() object, the new Game2() field MyFavourite empty, so null is displayed - MrFylypenko
  • Thank you very much! - alex6712
  • If the answer is correct, you should arrange it as an answer and mark it as correct. - Igor Kudryashov
  • And yes, you will not get the names of the games for you initialize them after writing to the collection - GenCloud

0