0

So basically, I'm trying to save several data saved from a file into an Array therefor I need to somehow get the size that the Array must be initialized to.

Is there any way to do that without creating another loop to count all the lines until the read line is null? Since that would require to re-open the file later which would not be very optimal. I'm trying to keep it as compact as possible.

Also, no I can't use ArrayLists.

MoveUK
  • 3
  • 1
  • 2
    Why not simply create an array of reasonably large size, and start adding data to it. If the data size becomes larger than the array, then do what ArrayLists do -- create a larger backing array and copy the data to it. – Hovercraft Full Of Eels Nov 10 '18 at 01:26
  • @HovercraftFullOfEels Since that would result in a lot of unused & wasted memory which is not a good practice – MoveUK Nov 10 '18 at 01:30
  • 1
    Just so we know all the bizarre restrictions in advance, what else can't you use? – President James K. Polk Nov 10 '18 at 01:30
  • Quite the opposite. It would give you enough memory when and where you need it. – Hovercraft Full Of Eels Nov 10 '18 at 01:30
  • @JamesKPolk that's pretty much all the restrictions, haha – MoveUK Nov 10 '18 at 01:32
  • You should look at the source code for ArrayList. It's not black magic, it's just an array that resizes itself as needed, and gives a little extra space to reduce the overhead of frequent resizing. – President James K. Polk Nov 10 '18 at 01:34
  • 1
    Otherwise you're talking about reading through the file *twice*, once to get the number of lines and then create the array, and again to add them to your array. Which carries more overhead? creating slots in memory or physically reading and re-reading a file? – Hovercraft Full Of Eels Nov 10 '18 at 01:36
  • *If*, almost impossible, but *if* storage is that big of deal then you can use the LinkedList class. Of course you lose the benefit of O(1) random access. – President James K. Polk Nov 10 '18 at 01:37
  • @JamesKPolk - A `LinkedList` will use more storage than an `ArrayList` if you are measuring the (peak) reachable storage over the lifetime of the data structure. (A `LinkedList` might win if you measure the amount of storage allocated over the lifetime ... but that's not usually a concern.) – Stephen C Nov 10 '18 at 02:12
  • You could use: `Path path = Paths.get(pathAndFileNameString); int linesCount = (int) Files.lines(path).count(); String[] fileLines = new String[linesCount];`. The **File.Lines().count()** method returns a long data type so you may want to cast to int. You will also need to surround the statement in a **try/catch**. – DevilsHnd - 退職した Nov 10 '18 at 02:35

2 Answers2

0

I would go for an ArrayList, as Haroon and Hovercraft.

However, if it is not an option, why not :

// 1. Create a new Set of String
Set<String> stringSet = new HashSet<String>();
// 2. Loop through bufferedReader and add lines to set
for (String line = br.readLine(); line != null; line = br.readLine()) {
stringSet.add(line);
}
// 3. Copy set to a new array
int n = stringSet.size(); 
String array[] = new String[n]; 
array = stringSet.toArray(array);

But don't expect huge performances.

AllirionX
  • 1,073
  • 6
  • 13
-1

If you know in advance how many lines there will be you create the array before the loop and then you will need a counter which you use an an index into the array. You increment the counter each time through the loop.

If the number of lines is variable then add the lines to an ArrayList in the loop and then convert the ArrayList to an array after the loop

Haroon
  • 538
  • 2
  • 11