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

[310 byte] By [riponc007a] at [2007-9-29 0:34:54]
# 1

void levi_h (int[] array, int length) {

if (array.length == 0) {

System.exit (123);

}

}

levi_ha at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 2
That question looks like it was written by someone who knows C but doesn't know Java.
YATArchivista at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 3

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));

}

}

dubwaia at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 4

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.

DrClapa at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 5

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

}

}

}

dubwaia at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 6

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

}

}

manowavea at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 7

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

GiddingsRa at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 8
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 ;)
levi_ha at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 9

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

Balavaa at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 10
> hmm, alot dubius looking recursize functions,That's a pretty dubious spelling of dubious. You really don't get it, huh?
dubwaia at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 11
No offence intended dubwai, just a play on the name :)
Balavaa at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 12
> No offence intended dubwai, just a play on the name> :)uh, sure.
dubwaia at 2007-7-13 3:02:36 > top of Java-index,Other Topics,Algorithms...
# 13

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 > top of Java-index,Other Topics,Algorithms...
# 14
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 > top of Java-index,Other Topics,Algorithms...