Getting rid of excess while statement

我只是一个虾纸丫 提交于 2020-01-06 08:26:12

问题


Could anybody have a look at this snippet of code and and tell me if there is a way to amalgamate the two while statements into one?

public static void main(String[] args) throws IOException
{
    BufferedReader fileInput;
    fileInput = new BufferedReader(new FileReader("information.txt"));
    int countOfClients = 0;
    while (fileInput.ready())
    {
        fileInput.readLine();
        countOfClients ++;
    }
    int totalClients = countOfClients ;

    Client[] clientDetails = new Client[totalClients];
    int clientNumber = 0;

    while (fileInput.ready())
    {
        String currentLineOfText = fileInput.readLine();
        String clientName = currentLineOfText.substring(0, 19);
        String gender = currentLineOfText.substring(20,21);
        char clientGender = gender.charAt(0);
        int clientAge = Integer.parseInt(currentLineOfText.substring(22,24));
        String clientInterests = currentLineOfText.substring(25);
        clientDetails[clientNumber] = new Client(clientName, clientGender, clientAge, clientInterests);
        clientNumber++;
    }

The first while statement is reading all the lines in the text, so it knows how many elements in the object array it needs.
The array clientDetails of class Client[] is then created.
The second while statement populates that array.
Can I avoid using two while statements?

Note: This is for an assignment and I have to use arrays.


回答1:


You could use an ArrayList instead of an array and simply use:

list.add(new Client(...));

If you really need an array, you can always call:

Client[] array = list.toArray();



回答2:


As they're all saying, use an ArrayList to store the items.

If memory is an issue, you can use ArrayList.toArray() to trim it down to the bare bones.

If efficiency is an issue, you probably shouldn't be reading from a file in the first palce.




回答3:


Why create an array ? Why not have one while loop that creates an ArrayList and then (if you need an array) extract the resultant array from that using ArrayList.toArray() ?




回答4:


You can avoid two while loops by changing Client[] to ArrayList();

Example:

List<Client> clientDetails = new ArrayList<Client>();
    int clientNumber = 0;

    while (fileInput.ready())
    {
        String currentLineOfText = fileInput.readLine();
        String clientName = currentLineOfText.substring(0, 19);
        String gender = currentLineOfText.substring(20,21);
        char clientGender = gender.charAt(0);
        int clientAge = Integer.parseInt(currentLineOfText.substring(22,24));
        String clientInterests = currentLineOfText.substring(25);
        clientDetails.add( new Client(clientName, clientGender, clientAge, clientInterests));
    }

Note: Hand edited, there may be syntax errors.




回答5:


If you really can't use the pre-written ArrayList class, you could always effectively re-implement it (or at least the relevant bits of it) yourself.

The key technique is to take a guess at the size of the array you might need, define an array that size, and, if you find it is too small, create a bigger array and copy all the existing values from the old to the new array, before continuing in the space that is left over.

At the other end of the loop, you might be in for yet another step, and shrink the array again (by declaring a smaller array and copying values over) so you have no empty spaces left.

Or, as recommended by all the other answers, just use an ArrayList, which already does exactly this for you...



来源:https://stackoverflow.com/questions/13418880/getting-rid-of-excess-while-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!