0

I'm trying to figure out how I would take the values of an array and pass it to another array. The image at the bottom is the array I was able to boil down to with crimeArrayNumbers. I am trying to take each index, like index 1 for example, and store it separately in an array held in another class. In this case, the array in another class if newUSCrimeArrays.population. Ideally, I'm trying to create an array for each column so that I can manipulate them and do math & search operations.

I've done a little research and thought that Arrays.copyOfRange or System.arraycopy() might work. However, I am not sure how to implement that properly right now. Is there an easier or more efficient method of doing this?

try {
    br = new BufferedReader(new FileReader(newUSCrimes.fileName));
    while ((line = br.readLine()) != null && !line.isEmpty()) {
        String[] crimesArray = line.split(csvComma);
        long[] crimesArrayNumbers = new long[crimesArray.length];

        //resting the index inside the main loop
        index = 0;
        for (int i = 0; i < crimesArrayNumbers.length; i++) {
            try {
                crimesArrayNumbers[index] = Long.parseLong(crimesArray[i]);
                index++;
                newUSCrimeArrays.population = Arrays.copyOfRange(crimesArrayNumbers, 0, 0);

            } catch (IndexOutOfBoundsException | NumberFormatException ioob) {
            }

        } //end for loop
        crimesArrayNumbers = Arrays.copyOf(crimesArrayNumbers, index);
        System.out.println(Arrays.toString(crimesArrayNumbers));
    }//end try
    System.out.println();
    System.out.println(Arrays.toString(newUSCrimeArrays.population));

} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
    if (br != null) {
        try {
            br.close();
        } catch (IOException e) {
        }
    }
}

Array

Marco Fincato
  • 153
  • 1
  • 7

1 Answers1

2

If I've understood well what you're trying to do, you want to import some data from a file to an array, then copy the content of this array into another array. If that's the case, you can use Arrays.copyOf(source, length). This will copy the source array, creating a new array with the given length. If the new length is less than the old one, the method will truncate, otherwise, it will fill the new part with nulls.

So, instead of doing this every iteration:

newUSCrimeArrays.population = Arrays.copyOfRange(crimesArrayNumbers, 0, 0);

You can just call, at the end of the for loop, something as simple as this:

newUSCrimeArrays.population = Arrays.copyOf(crimesArrayNumbers, crimesArrayNumbers.length);

An alternative to this, you could use clone(), which is a method inherited from Object. In arrays of primitives (int, bool...), this will do a deep copy of the content, meaning that will create a copy of every value. However, you have to be very cautious with this method cause it will behave differently if the array doesn't contain primitives.

Marco Fincato
  • 153
  • 1
  • 7
  • 2
    I recommend some reading about why `clone()` method shouldn't be used: 1. https://stackoverflow.com/questions/1106102/clone-vs-copy-constructor-vs-factory-method 2. https://www.artima.com/intv/bloch13.html 3. https://stackoverflow.com/questions/2597965/why-people-are-so-afraid-of-using-clone-on-collection-and-jdk-classes – Jaroslaw Pawlak Oct 14 '18 at 20:02
  • 1
    Thanks, I gave them a quick read. As I've specified, I suggested the `clone()` method for an array of primitives, I know it will do something different in every other case. I'll edit the question to clarify this. – Marco Fincato Oct 14 '18 at 20:11