I want to ask you if there is a way to set limit of an array elements number in java other then setting it in the beginning like int arr[] = new int[30]
( and the size is 30 elements). Can you set the limit between 1 and 10 elements with some condition like the following?
int[] array = new int[28];
Can you declare it somehow like this (it doesn't work, I tried several other options, but couldn't find the solution.
while (array.length <= 1 && array.length >= 30) {
return 0;
}
(or something like that).
And my second question that is maybe more viable is can you set a limit of the elements themselves, like when you need to sort them after and if an element is >= 100 to give an error. something like:
if (input <= 1 && input <= 150)
break;
CodePudding user response:
Create a List (ArrayList Or LinkedList or any other implementation fulfil your need), apply your condition on the list with Stream API and limit the size relevant to you business, then convert it to array simple by calling
list.toArray(Integer[]::new)
CodePudding user response:
You could use java ArrayList to do that things.
import java.util.ArrayList; // import the ArrayList class
ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList object