Can anyone help code this simple Java Problem?

Each row in a two dimensional array is itself an array. So the rows can have different lengths. A array if this kind is known as a ragged array. Define a method int[] trans(int[][]source) to get an array which stores all of the elements of source[][], and define another method print(int[]target) to show its elements in line. Examples I'm the picture.

Attachment image

1 Answer

Relevance
  • 3 weeks ago

    The way I'd write this is to loop through the (outer) source array once to add up all the lengths of "rows" in the ragged array.

    Then I'd create a result array of that's exactly big enough to hold all the elements in the rows of source; with a nested (inner) loop on the elements in each row to copy them to the result array.

    One place you can get some practice at Java arrays, strings, logic, etc. (the "small but important" bits of programming) is at Nick Parlante's "codingbat.com/java" site.  Parlante is a CS professor at Stanford, where he's apparently taught beginning classes in Java and Python.  You'll find links to instruction there that will help with the exercises there, and might well help you with assignments in your current class.

Still have questions? Get your answers by asking now.