How to create an ArrayList from array? Element[] array = {new Element(1), new Element(2), new Element(3)}; How do I convert the above variable of type Element[] into a variable of type ArrayList<Element>? ArrayList<Element> arrayList = ...;
There are different ways to delete an array element, where some are more useful for some specific tasks than others. Deleting a Single Array Element If you want to delete just one single array element you can use unset() and alternatively array_splice(). By key or by value? If you know the value andRead more
There are different ways to delete an array element, where some are more useful for some specific tasks than others.
Deleting a Single Array Element
If you want to delete just one single array element you can use unset()
and alternatively array_splice()
.
By key or by value?
If you know the value and don’t know the key to delete the element you can use array_search()
to get the key. This only works if the element doesn’t occur more than once, since array_search()
returns the first hit only.
unset()
Expression
Note: When you use unset()
the array keys won’t change. If you want to reindex the keys you can use array_values()
after unset()
, which will convert all keys to numerically enumerated keys starting from 0 (the array remains a list).
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
// ↑ Key of element to delete
Example Output:
[
[0] => a
[2] => c
]
array_splice()
Function
If you use array_splice()
the (integer) keys will automatically be reindex-ed, but the associative (string) keys won’t change — as opposed to array_values()
after unset()
, which will convert all keys to numerical keys.
Note: array_splice()
needs the offset, not the key, as the second parameter; offset = array_flip(array_keys(
array))[
key]
.
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);
// ↑ Offset of element to delete
Example Output:
[
[0] => a
[1] => c
]
array_splice()
, same as unset()
, take the array by reference. You don’t assign the return values back to the array.
Deleting Multiple Array Elements
If you want to delete multiple array elements and don’t want to call unset()
or array_splice()
multiple times you can use the functions array_diff()
or array_diff_key()
depending on whether you know the values or the keys of the elements to remove from the array.
array_diff()
Function
If you know the values of the array elements which you want to delete, then you can use array_diff()
. As before with unset()
it won’t change the keys of the array.
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = array_diff($array, ["a", "c"]);
// └────────┘
// Array values to delete
Example Output:
[
[1] => b
]
array_diff_key()
Function
If you know the keys of the elements which you want to delete, then you want to use array_diff_key()
. You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff_key($array, [0 => "xy", "2" => "xy"]);
// ↑ ↑
// Array keys of elements to delete
Example Output:
[
[1] => b
]
If you want to use unset()
or array_splice()
to delete multiple elements with the same value you can use array_keys()
to get all the keys for a specific value and then delete all elements.
array_filter()
Function
If you want to delete all elements with a specific value in the array you can use array_filter()
.
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_filter($array, static function ($element) {
return $element !== "b";
// ↑
// Array value which you want to delete
});
Example Output:
[
[0] => a
[2] => c
]
See less
Java 7+ In Java 1.7 or later, the standard way to do this (generate a basic non-cryptographically secure random integer in the range [min, max]) is as follows: import java.util.concurrent.ThreadLocalRandom; // nextInt is normally exclusive of the top value, // so add 1 to make it inclusive int randoRead more
Java 7+
In Java 1.7 or later, the standard way to do this (generate a basic non-cryptographically secure random integer in the range [min, max]) is as follows:
See the relevant JavaDoc. This approach has the advantage of not needing to explicitly initialize a java.util.Random instance, which can be a source of confusion and error if used inappropriately.
However, conversely with ThreadLocalRandom there is no way to explicitly set the seed so it can be difficult to reproduce results in situations where that is useful such as testing or saving game states or similar.
Java 17+
As of Java 17, the psuedorandom number generating classes in the standard library implement the
RandomGenerator
interface. See the linked JavaDoc for more information. For example, if a cryptographically strong random number generator is desired, theSecureRandom
class can be used.Earlier Java
Before Java 1.7, the standard way to do this is as follows:
See the relevant JavaDoc. In practice, the java.util.Random class is often preferable to java.lang.Math.random().
In particular, there is no need to reinvent the random integer generation wheel when there is a straightforward API within the standard library to accomplish the task.
See less