The strange behavior of contains , after a long debugging I noticed that it practically does not work, always returns false Here is the code:

 public static ArrayList<ArrayList<String>> cacheTemp=null; //ФУНКЦИИ СОЗДАНИЯ Array (к вопросу не относится) 

Further simple functions

 public static boolean getBoolean(int comnat){ //Если индекс записи существует должно вернутся true, но всегда возвращается false. if(cacheTemp.contains(comnat))return true; return false; } public static boolean setArrayList(int comnat, ArrayList<String> temp){ //МАССИВ КОМНАТ if(getBoolean(comnat)) { cacheTemp.set(comnat, temp); }else cacheTemp.add(comnat, temp); if(getBoolean(comnat)) { Voider.log("setArrayList", "true", "e"); //НЕ ВЫПОЛНЯЕТСЯ, ПО ЛОГИКЕ ДОЛЖЕН, СОЗДАН ДЛЯ ТЕСТА } return true; } 

I'm sending simple arguments,

 ArrayList<String> temp=new ArrayList<String>(); setArrayList(0, temp); 

and so on

  • Nothing is not clear, but probably you need to specify ArrayList <Integer> when creating - Android Android
  • Hmm, what you do not understand? In the line “Strange behavior” contains, after a long debugging, I noticed that it practically does not work, it always returns false .. PS I initialize when I need an array list, and the question was not why the application crashes (if there is null) and why false is returned to contains - Denis Kotlyarov
  • Tried something like this public static boolean getBoolean (int comnat) {// ROOM EXISTS? return cacheTemp.get (comnat)! = null; } but the fall - Denis Kotlyarov
  • 3
    read javadoc with the method contains. He does absolutely what you expect from him. It checks if the object passed in is listed. You have a list of racers. You check if an int is contained in an arreylist array. Whatever you send there, the answer will always be false, since int by definition cannot be contained in an array of ArrayList <String> - Vladyslav Matviienko

3 answers 3

Here you are not using the true contains, it checks for an object, i.e. looks for an element in the collection (integer) and you have ArrayLists there

 public static boolean getBoolean(int comnat){ //КОМНАТА СУЩЕСТВУЕТ? if(cacheTemp.contains(comnat))return true; return false; } 
  • It should check if there is an entry at all (in which the object) in the ArrayList <ArrayList <String >> list, but always false - Denis Kotlyarov
  • And, that is, it checks the existence of the contents, and then how to check the existence of the element? - Denis Kotlyarov
  • To find out the size of an ArrayList, use the size () method - Andrey Lobarev
  • Elements can be in discord, that is, 1 object 3 object 6 object - Denis Kotlyarov
  • And why such curly logic? - Stranger in the Q

It seems to work,

 public static boolean getBoolean(int comnat){ //КОМНАТА СУЩЕСТВУЕТ? return comnat >=0 && comnat < cacheTemp.size() && cacheTemp.get(comnat) != null; } 

right or wrong I don't know for sure but it works

It was a long time ago, the question is old, I do not recommend using it too slowly. ArrayList when using the get function will throw an exception if you are outside the array.

 ArrayList<Integer> list = new ArrayList<>(); list.add(1); try{ System.err.println(list.get(2)); }catch(java.lang.IndexOutOfBoundsException e){ e.printStackTrace(); //выбросить ошибку в error поток //что делать если вы вышли за пределы массива } 

    You need to rewrite the function as follows.

     public static boolean getBoolean(int comnat){ return cacheTemp.size() > comnat; } 
    • Elements can be in inconsistency, ie 1 object 3 object 6 object - Denis Kotlyarov
    • Why did you decide to use ArrayList and not a regular two-dimensional array? - Andrey Lobarev
    • Because it is more convenient, and it suits me, + I have a dynamic creation of objects, and it’s unknown how to redefine the array every time, I just don’t want to take this heresy myself ... Why invent something invented?) - Denis Kotlyarov