object ArrayFrom2DArrayNonZero {
def main(args: Array[String]): Unit = {
val array = Array(
Array(1, 0, 8, 2),
Array(0, 7, 3, 0),
Array(9, 0, 0, 4)
)
val nonZeroBuffer = scala.collection.mutable.ArrayBuffer[Int]()
// Collect non-zero elements
for (row <- array) {
for (element <- row) {
if (element != 0) {
nonZeroBuffer += element
}
}
}
// Convert the list back to a one-dimensional array
val resultArray = nonZeroBuffer.toArray
resultArray.foreach(element => print(s"$element "))
}
}
/*
run:
1 8 2 7 3 9 4
*/