There is a doubly linked list (double linked list?) In java. It creates a linkedList list = new linkedList()
. It is filled with random int values, the length can be specified by the user. It is necessary to find the number of elements that are in the interval [-100; 100]. How to organize the cycle of searching for elements is more or less clear, but how to get the values of the elements of the list in order to subsequently determine their belonging to the interval?
- 2.get (i)? or even for (Integer u: list) or did I misunderstand the question? - pavel
- When I enter .get (), the program complains that "method get () is undefined .." - Marqiz
- and for works? get - yes, in the clean lists there is no, there is only an iterator to walk. - pavel
- And how to write this for? Now I’m getting type mismatch - can't go - Marqiz
- @Marqiz, add a code to whom you are trying to solve the problem - it will be easier to indicate where something is wrong - YuriySPb ♦
|
1 answer
int count = 0; for (Object num : list) if ((Integer)num >= -100 && (Integer)num <= 100) count++; System.out.println("Всего элементов в заданном диапазоне: " + count);
int count = 0; for (int i=0; i<list.size(); i++) if ((Integer)list.get(i)>= -100 && (Integer)list.get(i) <= 100) count++; System.out.println("Всего элементов в заданном диапазоне: " + count);
No need to get them. It is in the map
values are stored inside Map.Entry
, and here they are wrapped in nothing, they are as they are. Just your List
not typed, it stores Object
, which still need to be cast to the desired type. If you had a List<Integer>
, then you would not need to perform a type conversion, and there would be no error.
|