programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# incrementing arrays

Ranch Hand
Posts: 33
Hi again. Is it possible to increment an array? I need to determine what amount of 12 has a certain characteristic, with the others not having that characteristic. For instance, consider the following code snippet:
int z = 0;
if (myMonth [z].Days == 31){
myCount z ; //myCount being declared as a new array for the counter
System.out.println("These months have 31 days:");
System.out.println(myMonth [z].Name);

How do I go about incrementing the array? I tried z++ and myCount [z++], both to no avail. Any help is again appreciated.
Thanks.

Ranch Hand
Posts: 410
I'm a bit confused by your question: are you asking how to perform the same operation on every element of an array? If so, you need to increment the index to the array, not the array itself. This is called iterating through the array. For example:

Here the index is the variable "i", and it is increased automatically each time the for loop repeats. So the first time the loop will print the name of myMonths[0]; the second time myMonths[1] will be printed, and so on up to and including myMonths[9].

You can do something very similar by placing your if statement inside a for loop.

Dave Kairys
Ranch Hand
Posts: 33
I have 12 total elements to loop through. I need to pick out which months have 31 days and which have less than 31 days. The output will be as follows:
These months have 31 days:
January
March
.
.
December

These months have less than 31 days:
February
.
.
November
I used the following code to get these results:
January has 31 days
.
.
December has 31 days
int x = 0;
while (x < myMonth.length){
System.out.println(myMonth[x].Name + " has " + myMonth [x].Days + " days.");
x ++;
I need to use something similar to get the first listed output but I'm not quite sure how to do it. I know I need to keep track of the months with 31 days and be able to print it out as above; same with those with less than that. I would like to use something similar to:
int z = 0;
if (myMonth[x].Days == 31) {
(code to keep track of months goes here, but I don't know what it is)
} else {
code for months with <31 days here

Hopefully this clears it up a bit.
Thanks!

Ranch Hand
Posts: 4632
you want to create 2 sub-lists, of unknown size, so use AtrrayLists

java.util.List days31 = new java.util.ArrayList();
java.util.List daysLessThan31 = new java.util.ArrayList();

now iterate sub-list days31
System.out.println((String)days31.get(x));

now iterate sub-list daysLessThan31
System.out.println((String)daysLessThan31.get(x));

Dave Kairys
Ranch Hand
Posts: 33
Thanks for the response, Michael. One more question- According to your code snippet, how can I add the monthname when I don't know what it is? Looking at your code and from my experience with .NET(I know that Java is different, but some of the methods are similar in structure), you need to know what it is that you are adding. Is that where the ArrayList comes into play?

Thanks again,
Dave

Ranch Hand
Posts: 3061
It looks like you DO know what the name is. According to the code you gave above, you can get the name as "myMonth[x].Name". Michael's solution stores the names in two different lists based on the criteria you gave. You could just as easily print the names out without creating the Lists. This would probably mean that you need to iterate through the array of months twice depending on the exact format you need for the output.

Layne
[ September 11, 2005: Message edited by: Layne Lund ]

Layne Lund
Ranch Hand
Posts: 3061
p.s. Are you familiar with while and for loops? I suspect this is a major tool that you are missing to complete this assignment. If you don't know what these are, look in your textbook or google for more information. We will be glad to clarify anything that you don't understand.

Layne

Dave Kairys
Ranch Hand
Posts: 33
Yes I am familiar with iteration, but the syntax that Java requires is throwing me for a bit of a loop. I have more experience with VB flavors; iteration is more straight-forward with those languages for me than it is in Java. I am getting the hang of it now, slowly. Thanks for the help and the clarification, if needed. The Java gang really is truly a helpful group!

Thanks!
Dave

 There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS