简体   繁体   中英

The first number inputted indicates the number of elements in the list java

Here's my question:

I need help writing a program where the first number inputted indicates the number of elements in the list. This number is not part of the list. Therefore, each list can be of any length, as long as the first number entered binds the length of each list.

How can I can code this to be on the same line? Normally I would ask user for length before entering array list.

Thanks,

Something like : for( int i=1;(!list.isEmpty() && i<list.get(0)); i++)); // Assuming list contains Integer . List< Integer > list and i=1 to skip the first element

Note: I did not check the code, since i do not have any IDE currently.

Update: Above condition can be modified as below, though i can onlky presume but cannot test it: Try it if it helps you. Take care of syntax.

for( int i=1;(i==1?(!list.isEmpty() && i < list.get(0)):i < list.get(0); && i < list.get(0)); i++));

I think this will help you :

private List<Integer> listElements(int lenght){

   return new ArrayList<>(length);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM