You cannot make an empty array and then let it grow dynamically whenever the user enters a number in the command line. You should read the numbers and put them in an ArrayList instead. An ArrayList does not require a initial size and does grow dynamically. Something like this:
public void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
BufferedReader console =
new BufferedReader(new InputStreamReader(System.in));
while(true) {
String word = console.readLine();
if (word.equalsIgnoreCase("end") {
break;
} else {
numbers.add(Integer.parseInt(word);
}
}
Ofcourse you won't use while(true)and you won't put this in main, but it's just for the sake of the example
You cannot make an empty array and then let it grow dynamically whenever the user enters a number in the command line. You should read the numbers and put them in an ArrayList instead. An ArrayList does not require a initial size and does grow dynamically. Something like this:
public void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
BufferedReader console =
new BufferedReader(new InputStreamReader(System.in));
while(true) {
String word = console.readLine();
if (word.equalsIgnoreCase("end") {
break;
} else {
numbers.add(Integer.parseInt(word);
}
}
Ofcourse you won't use while(true)and you won't put this in main, but it's just for the sake of the example
How about:
Scanner scan = new Scanner(System.in);
System.out.print("Enter the array size: ");
int size = scan.nextInt();
int[] yourArray = new int[size];
//can even initialize it
Arrays.fill(yourArray, -1);
How do I create an empty array in Python?
How do I check if an array is empty?
How do I declare an empty array of size n in Python?
If you are going to use a collection that you don't know the size of in advance, there are better options than arrays.
Use a List<string> instead - it will allow you to add as many items as you need and if you need to return an array, call ToArray() on the variable.
var listOfStrings = new List<string>();
// do stuff...
string[] arrayOfStrings = listOfStrings.ToArray();
If you must create an empty array you can do this:
string[] emptyStringArray = new string[0];
As of .NET 4.6, the following would be recommended for an empty array:
String[] a = Array.Empty<string>();
With C# 12/.NET8, there's an even more succinct way (using collection expressions):
String[] a = [];
In .NET 4.6 the preferred way is to use a new method, Array.Empty:
String[] a = Array.Empty<string>();
The implementation is succinct, using how static members in generic classes behave in .Net:
public static T[] Empty<T>()
{
return EmptyArray<T>.Value;
}
// Useful in number of places that return an empty byte array to avoid
// unnecessary memory allocation.
internal static class EmptyArray<T>
{
public static readonly T[] Value = new T[0];
}
(code contract related code removed for clarity)
See also:
Array.Emptysource code on Reference Source- Introduction to
Array.Empty<T>() - Marc Gravell - Allocaction, Allocation, Allocation - my favorite post on tiny hidden allocations.
1) To create new array which, you cannot iterate over, you can use array constructor:
Array(100) or new Array(100)
2) You can create new array, which can be iterated over like below:
a) All JavaScript versions
- Array.apply:
Array.apply(null, Array(100))
b) From ES6 JavaScript version
- Destructuring operator:
[...Array(100)] - Array.prototype.fill
Array(100).fill(undefined) - Array.from
Array.from({ length: 100 })
You can map over these arrays like below.
Array(4).fill(null).map((u, i) => i)[0, 1, 2, 3][...Array(4)].map((u, i) => i)[0, 1, 2, 3]Array.apply(null, Array(4)).map((u, i) => i)[0, 1, 2, 3]Array.from({ length: 4 }).map((u, i) => i)[0, 1, 2, 3]
var arr = new Array(5);
console.log(arr.length) // 5