Solved

how to terminate printing an array of ints

Posted on 2004-09-29
10
191 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 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
Hrmm, still working w/ it. How exactly does that && statement work in the for loop? Like what's the logic behind that.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

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

Author Comment

by:NeedlessKane
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>  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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now