Solved

how to terminate printing an array of ints

Posted on 2004-09-29
10
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 92

Accepted Solution

by:
objects earned 25 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 25 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

691 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