?
Solved

how to terminate printing an array of ints

Posted on 2004-09-29
10
Medium Priority
?
209 Views
Last Modified: 2010-03-31
Hello,

I have this array of 0s and 1s. Originally the array is initialized to all 1s, and then using this method:

public static int bitSwap(int[] swapArray)
{
      
for (k=0; k < swapArray.length; k++)
{

if (swapArray[k] == 0)
{
swapArray[k] = 1;
}
else
{

swapArray[k] = 0;
j=k;
break;
}

}

It switches 1s to 0s and 0s to 1s, but once a 1 has been flipped to 0, the code breaks. This method returns the index at which the 0 was flipped, which is what i need it to do. The user passes an integer value, which determines the size of the array that is passed to this method. My question is this:

I need to print the return statement of this method in a loop of some kind. I know that when the array passed contains ALL 0s in it's indices, then it needs to stop, but I can't figure out how to make it do that.
Help would be appreciated.

-NeedlessKane
0
Comment
Question by:NeedlessKane
  • 5
  • 4
10 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 12184916
heres a method to print the array contents.:

public static void print(int[] a)
{
   for (int i=0; i<a.length; i++) System.out.print(a[i]+" ");
   System.out.println();
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12184933
use something like the following in your loop to display the processing:

print(a);
int result = bitSwap(a);
System.out.println(result);
0
 

Author Comment

by:NeedlessKane
ID: 12185003
Yeah, but I need to keep printing the values of bitSwap until the array that is passed is full of 0s, how can I make it check to see when the array is all 0s?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Expert Comment

by:objects
ID: 12185047
you'll need to check the array contents after each call.

boolean allzero = false;
for (int i=0; allzeroes && i<a.length; i++) if (a[i]==1) allzeroes = false;
0
 

Author Comment

by:NeedlessKane
ID: 12185192
Hrmm, still working w/ it. How exactly does that && statement work in the for loop? Like what's the logic behind that.
0
 
LVL 92

Expert Comment

by:objects
ID: 12185213
&& is an and, so the expression is only true if both the expressions are true
0
 

Author Comment

by:NeedlessKane
ID: 12185400
This is what i'm trying and it's not working:

int d;
boolean allzero = true;
for (d=0; allzero && d<diskMove.length; d++)
bitSwap(diskMove);
if (diskMove[d]==1)
allzero = false;
else{
System.out.println(diskMove[d]);
     }


0
 

Author Comment

by:NeedlessKane
ID: 12185846
Still not working, i've tried alot of things, agh.

All i need to do is run that bitSwap() in a loop and stop it each time....then after it stops, check the current array to see if all the values are == 0, if they aren't == to 0, run bitSwap again, and keep doing that over and over, but i'm at a loss.
0
 
LVL 3

Assisted Solution

by:CI-Ia0s
CI-Ia0s earned 100 total points
ID: 12186209
I'd add "allzero = true;" to the top of the else statement:

int d;
boolean allzero = true;
for (d=0; allzero && d<diskMove.length; d++)
bitSwap(diskMove);
if (diskMove[d]==1)
allzero = false;
else{
allzero = true;
System.out.println(diskMove[d]);
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12186854
>  if they aren't == to 0, run bitSwap again, and keep doing that over and over, but i'm at a loss

your call to bitSwap needs to be outside your loop.

> I'd add "allzero = true;" to the top of the else statement

Not really necessary, as allzero is already true.

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question