Since [] is an operator and java does not support operator overloading you can't use it with List. Instead you have to use the set(int index, T value) and get(int index) methods, which may be verbose but provide exact the same functionality.
Since [] is an operator and java does not support operator overloading you can't use it with List. Instead you have to use the set(int index, T value) and get(int index) methods, which may be verbose but provide exact the same functionality.
List.get(int) allows you to access elements using the index.
Java while loop to find index of elements in ArrayList
A better way to find the index of an element in an array?
Videos
I'm sorry for the possibly bad title! Didn't know what else to put there.
So my problem is, I am doing the MOOC course for java from University of Helsinki, and my challenge was to create a program which reads input from user and puts it on an ArrayList. If the user enters -1, the reading will end. After the list has been created (user enters -1), the program will ask the user for number and print out all the indexes where that number exists in the ArrayList. I have blasted my way through all of the earlier challenges but just can't manage to wrap my head around this. I do know how to solve this with for-loop and arrays, but I'm supposed to solve it using a while loop and without the use of arrays.
My current code is this:
if(list.contains(number)) {
while(i < list.size()) {
spot = list.indexOf(number);
i++;
}
System.out.println("Number " + number + " is in the index " + spot);
} and it works just fine when I have a list that contains only 1 instance of a number. But when I have a list that has for example two times the number eight in different indexes, it will only return the first one. Any suggestions?
Currently the best way I have found is to typecast the array into an ArrayList, and using ArrayList's method .indexOf to get the index, for e.g:
import java.util.Arrays;
int[] arr = {2,3,4,5};
Arrays.asList(arr).indexOf(4); // returns 2Is this the best way? Or unnecessary wrapping?
I have a list of objects stored in ArrayList. I want to find one element in this list based on a condition and shift it to the beginning of list. Here's how I'm dong it right now:
reOrderList(List<Room> roomList, int personId) {
for(Room room : roomList) {
boolean found = false;
for (Person person : room.getPersonList()) {
if (person.getId() == personId) {
found = true;
break;
}
}
if (found) {
Room foundRoom = room;
list.remove(room);
list.add(0, foundRoom);
break;
}
}
}The room and person class look like this:
class Room {
List<Person> personList;
}
class Person {
int Id;
}As seen from the code, I have a person's id, I want to check which room in the list contains that person and then shift that room to the start of room's list.
I have done a very basic implementation above, just want to know if there can be more refined way of doing this using any in-build features of Java? I meant can I somehow use stream or forEach, I used for loops because I needed break statement as there will be only one element to be found. And is there any other way of changing index of element to the beginning rather than removing and appending at the starting?