I am currently making a tiny little program that should be able to select a random element i put into the array using a Random of course (For practice purposes) and when a element in the array has been chosen at random. i want to remove this element in the array, so how do you remove a element in a array the easiest way?
Its the only thing i want to know. I got everything else sorted. It's just removing the element it has chosen (the random takes a random number between 0 and the amount of elements in the array, so if it chooses 0, it will take the first element in the array, and so on)
You can use ArrayList which support remove or add function, which actually is an resizable array.
You can't remove an element from an array. You can replace it with some other value that indicates "nothing", null for example.
An easy solution is to convert the array into a list.
list = Arrays.asList(array);
Remove any element from the list and then revert it back to an array using
array = list.toArray();
Hope it helps.
Use List instead of Array, and if you want to stay on Array than there is 2 solution,
Create another Array ignoring your element which you want to delete.
create a List using Array.asList(...) than remove element from list and convert back to Array.
but according to me its better for you as well as java to use List. because List provide a many build-in functions.
Related
I am using an input file to populate my array with different strings. how do I check up to where the array is filled? I initialize the size of my array to 30000, but how do I check up to which index it contains a string?
Thank you for your help!
Low-level approach: while populating the array, maintain a count variable, adding 1 to it on every element you insert into the array.
Professional approach: instead of an array, use a java.util.List, e.g. a java.util.ArrayList. List instances do the counting for you (and by the way, don't waste 25000 empty elements if your file only contains 5000).
so i have an array list that contains strings such as:
ArrayList<String> list = new ArrayList<>();
list.add("bookshelf");
list.add("bookstore");
list.add("library");
list.add("pencil");
Now i wanna search and remove all the strings in the arraylist that contain the word "book" in them. As far as i understand list.remove("book"); will only search for the particular string "book" and not the strings that contain the word "book". How can i solve this?
You can use removeIf like this:
list.removeIf(s -> s.contains("book"));
Note: this answers applies to Java version 7 and below (of course that it will work for higher versions as well but YCF_L's answer is simpler to implement in versions 8 and above).
The requirement is to iterate the list, check every element, and if it answers a certain condition: remove it.
Since this is the case we fall into a risky scenario where we modify the list while iterating it which is problematic because when we remove an element in the list its size changes.
In order to work around this problem we can iterate the list by index from the last element and back until the first one, this way, removing an element at index n will not effect accessing any element at index < n.
I'll leave the implementation details to you in order not to "spoon feed" and destroy your exercise :)
So, I was reading my textbook about linked lists. So if it isn't a special case if something is inserted at the end of a linked list, is it a special case, by way of contrast if an element is inserted at the end of an array? What is the reason behind the answer. Why? Why isn't it a special case when inserted at the end of a link list and why is/isn't a special case at the end of an array? What about for ArrayLists?
It is also not a special case for Array (if we are not talking about exceeding bounds of Array). But for ArrayList it could be a special case, because ArrayList has a capacity and if it is exceded ArrayList needs to be resized in order to make sure that it has space for inserted element.
Arrays have a fixed size that you give at initialization (This part is important because array elements are stored at consecutive memory addresses). Hence, you cannot append an element to an array without creating a new array.
int[] myArray = new int[10];
myArray.append(42); //Where should it be stored? It would not make sense.
For lists, it depends on the list implementation. The LinkedList consists of several Node connected to each other. Hence, adding an element to the end of the list is equivalent to connecting a new Node to the last Node of the list.
Is is possible to get out of an ArrayList the first number of the first index?
Here's an Example:
In there are 5 items:
path = {0.5,5.0},{0.6,6.0},{0.7,7.0},{0.8,8.0},{0.9,9.0}
And I want to get the number 5.0 out of {0.5,5.0}...
I tried it with path.get(0) But it only gives me {0.5,5.0} back.
Is it possible to get 5.0 out of it without getting all the other numbers?
If your ArrayList contains arrays, this is the way to go
myList.get(0)[1] // You're getting the index 1 from the 1st array of your ArrayList
Otherwise, if it is containing other ArrayList's
myList.get(0).get(1) // Same logic as above applied to the Collection
If your curly braces in the ArrayList are actually brackets (they probably are), you can use:
myArray.get(0)[index] to get the index you want. In your example it is:
myArray.get(0)[1];
Note: if your ArrayList elements are also ArrayList then you need to use get(0).get(1) instead of get(0)[1].
I am trying to convert a section of code from using an ArrayList of custom objects to a regular array.
Previous my definition was
ArrayList<Room> rooms = new ArrayList<Room>();
Which I have now changed to
Room[] rooms;
Previously I used the below line to add items to the array list
rooms.add(new Room(1,1,30,false,true,true,false));
But I am now struggling to find the way I should simply add individual items to the array throughout code.
I think you are best sticking with an arrayList here, but just to give you a bit more light on it.
To do what you are trying to do, you will have to keep a index integer which will just point to the current position in the array, then when you add you can increment this and add the new object into the next poisition.
When you get to the maximum size of your array, you will need to expand it.
You will find that there has been questions on expanding an array which have been asked already and you can find the answers here:
Expanding an Array?
If you can live with a fixed-size array, that gives you at least a slight chance of success. If not, you can't beat ArrayList and if your mission is to succeed without reimplementing it, then it is an impossible mission.
You should really give more insight into the exact rationale for rewriting your code like that, it would give us some chance to properly help you.
I can recommend:
Use Arraylist as Long you Need to insert Elements. Once th Array is final Convert to Array.
If you need to increase the size of the array when adding another element, you have to construct another array with the size of the old array +1.
Afterwards, you would copy the contents of the old array over to the new array (the bigger array).