Dear All:
I use a method to compare two arrays of type String[]. Any String Value that is not on both arrays I want to be placed in a new array.
I am not using any cool methods from the API because I am learning, so compare this to a kid with a calculator v a pen and paper.
(I do use "equals" because I can not work out how not to )
So in the example I have below I have created the array because I know the answer, there should be one value.
"String[] newlyLargerPopulation = new String[1];"
ANSWER I KNOW IS OUT THERE BUT SEEMS LIKE A WASTE AND BAD CODE
1. Run the method in a dummy run mode.
2.Record how many String entries will pop up that are not on both lists.
3.Create the Array now that I know how long it needs to be.
4.Run the method again and fill the array.
QUESTION
How do I get around the need to create the array to capture the values, when I do not know how long the array will need to be to capture all the values.
OTHER PERTINENT INFORMATION(OPI)
1. It may be that none of the information in either of the two arrays being compared is the same nor will the length of the arrays always be the same or similar.
2. I will ad an if statement to make the calculation work to always check the longer array against the shorter array, but this is not important here.
import java.util.Scanner; import java.util.*; public class ArrayQuestion { public static String[] overtakenBy(String[] previous, String[] viable) //Method compares two arrays and returns an array of type string. { String[] newlyLargerPopulation = new String[1]; //The problem, I have to set the arrays length, but in the real world I // will not know what to set the arrays length to. for(int b = 0 ; b < viable.length ; b++) // Stepping through the Viable arrays values. { int x = 0; //Counter used to trigger the placement of a String value into the "newlyLargerPopulation" array. for (int i = 0 ; i < previous.length ; i++) //Stepping through the previous arrays String Values, checking each one against // the viable arrays string value. { if (viable[b].equals(previous[i])) // Do the values in the respective arrays match? System.out.println(viable[b] + " equals " + previous[i]); //Place holder to let me see what is going on, not needed by the code. else { x++; //If the name did not match then "x" is increased by one, when "x" gets to the same System.out.println("x = " + x); // value as previous[].length, this triggers the addition of the String value to the if ( x == previous.length) // array that is returned by the method. { newlyLargerPopulation[0] = new String(viable[b]); //value being added to the array that is returned. } else; } } } return newlyLargerPopulation; //returning the new array. } public static void main(String[] args) { String[] previousYear = {"Dog", "Cat", "Rabbit"}; //Creating the previousYear array. String[] viableSpecies = {"Dog", "Cat", "Rabbit", "Terrapin"}; //Creating the viableSpecies array. String[] theDifference = overtakenBy(previousYear, viableSpecies); //Comparing the two and returning an array of type String. System.out.println("The name of the species that newly overtook our species by population is the " + theDifference[0]); //Output for my sake to see that it worked under // these controlled conditions. } }