An array isn't a primitive; it is an object. I wouldn't think about simply using primitives, because you are going to dissociate the name of the city from the count. You can have a VisitCounter class with name and visitCouint fields (or similar). You can have an array of ints and count “random” numbers like this:-You can also collect (boxed) numbers into a Map<Integer, Long> with a combination of Collectors#groupingBy() and Collectors#counting(), but the resultant count will be a Long rather than an int.
I was looking closer at your problem to see how to get frequency out of your input. One thing I noticed is that you have two loops. An outer loop which is the number of trips and an inner loop that is the number of cities for a given trip. I prefer to separate responsibilities so rather than cram all the logic into these loops I'd break the responsibilities out, something like: input all trips/cities, print all trips/cities, calculate frequencies, print frequencies. Ideally you would put those responsibilities in separate methods. In order to separate responsibilities you'll need to start with an array of arrays where the rows are the trips and the columns are the cities for that trip.
You need to have a way to store the city name and its associate count. There are other ways to do this but it is highly recommended that you make a very small class that can hold a String and an int. String being the name of the city and int being a count of the number of times that particular city appears.
In the main program you will have to allocate an array to hold a number of these objects. How big an array? Well, the worst case scenario is that each city only appears once, so you'll have to go through the trips and add up the number of cities in each trip.
Now, if you have any city that appears multiple times the array won't be entirely filled in, so you need a counter to track how many in the array have actually been populated.
Then, traverse all the cities in all the trips and for each city look to find the entry in the array for that city. If you don't find one then add one. If you do find one then increment its counter.
Thank you and sorry for the late reply.
So you mean that everyone I enter a new city I have to traverse the array of the string-integer object (like artisanal hashmap, in other words?) to see if the string is already here?
How would I increment the count?
Yes, a linear traversal of an array to find a city is crude but it says within your requirements of sticking to basics like arrays. The speed is not as bad as you might think for reasonably sized data sets.
int count = 0;
// add new city
frequencies[ count++ ] = new Frequency( city ); // constructor count should default to '1'