T
T
thatmaniscool2021-10-22 09:53:09
Java
thatmaniscool, 2021-10-22 09:53:09

How to get the size of a string array?

I am writing software for accounting for user equipment in a warehouse. Accounting should be kept in the database, but while there is no database, and you need to write further and test too, then I decided to write a class for random creation of user equipment objects.
I have a string array of data:

<string-array name="modelOfPersonalComputersStringArray">
        <item>HP ProDesk 400 G5 MT </item>
        <item>HP Compaq Elite 8300 CMT</item>
        <item>HP EliteDesk 800 G2 SFF</item>
        <item>HP Z6 G4 Workstation</item>
        <item>HP ZBook 17 G3</item>
        <item>HP EliteDesk 800 G2 SFF</item>
    </string-array>


In the class, I create a method to get a random value from a string array:
String [] tempArray = context.getResources().getStringArray(R.array.modelOfPersonalComputersStringArray);

The string array receives data, but the number of array elements tends to the maximum possible number.
How to correctly get the real size of a string array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BorLaze, 2021-10-22
@thatmaniscool

This is not a random value from the array. This is an array of all strings in modelOfPersonalComputersStringArray.
If you need one random string, then you need to take

Random random = new Random();
String s = tempArray[ random(tempArray.length) ];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question