Print an array
hi all,
Please help me to solve the problem using Java.
Problem: Write a recursive method printArray that takes an array and the size of the array as arguments and returns nothing. The method should stop processing and return when it receives an array of size zero.
Thanks,
Ripon
void levi_h (int[] array, int length) {
if (array.length == 0) {
System.exit (123);
}
}
That question looks like it was written by someone who knows C but doesn't know Java.
public static void printArray(Object[] array, String value)
{
try
{
value += array[0];
printArray(array, value);
}
catch (Throwable t)
{
char[] message = {103, 111, 32, 102, 117, 99, 107, 32, 121, 111, 117, 114, 115, 101, 108, 102};
System.out.println(new String(message));
}
}
public void printArray(Object[] array, int size) {
if (array.length != 0) {
while (true) {
// don't return unless the length of the array is zero
}
}
}
Oops... not recursive.
Easily fixed:
public void printArray(Object[] array, int size) {
if (array.length != 0) {
while (true) {
printArray(array, size);
// don't return unless the length of the array is zero
}
}
}
dubwai, that's slightly inefficient, as both the array length and the while guard are invariant for a given invocation. May I suggest, the tighter and easier to follow:
public void printArray(Object[] array, int size) {
while (array.length != 0) {
printArray(array, size);
// don't return unless the length of the array is zero
}
}
> hi all,
> Please help me to solve the problem using Java.
>
> Problem: Write a recursive method printArray that
> takes an array and the size of the array as arguments
> and returns nothing. The method should stop processing
> and return when it receives an array of size zero.
>
> Thanks,
> Ripon
Errr, why recursive and just generally why would you want to do this?
Debugging :)I just noticed that my method wasn't recursive either :$ - you'll just have to believe me if I say I can do that too ;)
hmm, alot dubius looking recursize functions,
A recursive function has to have a base condition from which to return
this doesn't, its an infinite loop because size is not decrimented
public void printArray(Object[] array, int size) {
while (array.length != 0) {
printArray(array, size);
}
}
this is a bit better
public static void printArray(Object[] array, int size) {
while (size != 0) {
System.out.println("Size=" + size);
size = size -1;
printArray(array, size);
}
}
but look at the output
Size=3
Size=2
Size=1
Size=1
Size=2
Size=1
Size=1
Here is a no thrills exampl and test main method
public class Recurse {
public static void main(String[] args) {
Object[] array = new Object[] {"1", "2", "3"};
Recurse.printArray(array, array.length);
}
public static void printArray(Object[] array, int size) {
if(size == 0) {
return;
} else {
System.out.println("Size=" + size);
size = size -1;
printArray(array, size);
}
}
}
output
Size=3
Size=2
Size=1
> hmm, alot dubius looking recursize functions,That's a pretty dubious spelling of dubious. You really don't get it, huh?
No offence intended dubwai, just a play on the name :)
> No offence intended dubwai, just a play on the name> :)uh, sure.
I realize this thread is kind of dead, but for all those interested. Here is how to print an array, forwards:
public void printArray( Object[ ] array, int size)
{
if( size-- == 0 )
{
return;
}
else
{
printArray( array, size );
System.out.println( array[ size ] );
}
}
Hope this helps anyone who needs it!
Linka at 2007-7-13 3:02:36 >

Nitpick: that's one way to do it if you must do it recursively.This however works fine for me:System.out.println(Arrays.toString(new int[] {1, 2, 3, 4, 5, 6}));
dwga at 2007-7-13 3:02:36 >
