1

I'm trying to populate a numbers array of long numbers (as well as a temp array of the same numbers) by reading in a file. The file is about 32000 lines of various long numbers (one per line). The code provided by my professor looks like this:

public static void main(String[] args) throws FileNotFoundException {

    Scanner in = new Scanner(new File("longNumbers.txt"));
    Long [] numbers;
    Long [] temp;
    Long startTime, endTime;

    while(in.hasNext()) {
        //TODO:  populate numbers and temp arrays

    }

With the work implied to be done within the while loop provided. My question is this: isn't an array's size fixed? And if we haven't scanned in every line before populating the array, how do we know what the size of the arrays will be? I'm very confused as to how these two arrays should be populated/instantiated while checking each line.

7
  • Use a ArrayList or other List of you need a variable size. Commented Oct 12, 2017 at 18:31
  • You could construct a List at first and then convert it to an array after. Other options would be to use the new streams somehow (haven't looked into them as much) Commented Oct 12, 2017 at 18:32
  • 1
    @GarrettMcClure Then you'll need to either allocate a ton of memory upfront, or do what ArrayList does and manually reallocate once the Array gets full. There's a reason the ArrayList class and other abstractions exist. Manually dealing with arrays can be a pain in many cases. Commented Oct 12, 2017 at 18:34
  • 1
    Just realized you could also first look at the file, figure how many lines long it is, then use that information to know how many slots to allocate. Commented Oct 12, 2017 at 18:44
  • 1
    You are obviously looking at a solution that uses the provided code. Possible solution : Initialize numbers [] to some size :: when full initialize temp to numbers.length :: copy from numbers to temp :: reinitialize numbers to old size + x :: copy back from temp :: continue iterating. Commented Oct 12, 2017 at 18:48

3 Answers 3

3

You can use dynamic sized array ArrayList<Long> Basically a wrapper over an array and takes care of resizing and copying it when you are trying to write out of bounds. Sequential memory allocation in nature and comes up with cost of re-creating array and copying elements if rough estimate is not known and too much re-sizing is done.

or LinkedList here

Sign up to request clarification or add additional context in comments.

2 Comments

I'm pretty sure we're meant to use only arrays, as that is what was provided by my instructor. Otherwise, I would have immediately switched to an ArrayList.
I think your professor wants you to come up with ArrayList implementation or the other way would be to iterate through your file 2 times.
1

I think your instructor also talked about how to resize an array. Here is a short refresh:

int INIT_SIZE = 10
int[] array = new int[INIT_SIZE];
i = 0
while(condition)
{
    array[i++] = nextInt();
    if(i == array.length)    
         array = Arrays.copyOf(array, 0, array.length + (array.length / 2));
}

It is a common solution to resize an array adding half of its length.

Now that you know how to resize an array of int it is going to be easy to resize an array of Long.

Comments

0

You could try in.nextLong() in your loop with an ArrayList and create an array with the size of the ´ArrayList´ afterwards (if you really need it):

ArrayList<Long> numbers = new ArrayList<Long>();
while (in.hasNext()) {
    numbers.add(in.nextLong());
}
long[] numberArray = numbers.toArray(new Long[numbers.size()]);
// put all the numbers from ArrayList to the array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.