Search code examples
scalalistbuffer

Scala ListBuffer data not accessible


I have the following code:

 val lines =  Source.fromFile("/home/cloudera/file_01").getLines().drop(1).toVector

I get a vector of file lines - which is file:

lines: Vector[String] = Vector(blk1|20170912|||||, ||||||, |c1|c2|c3|||, |201710|201711|201712|||, v1|1|4|9|||, v2|1||7|||, blk2||||||, ||||||, c4|c5|c6||||, |201710|201711|201712|||, h|h|h|h|h|h|h, j|j|j|j|j|j|j, k|k|k|k|k|k|k, m|m|m|m|m|m|m, ||||||, ||||||, ||||||, 201801|1|5||||, 201802|9|100||||, 201803|9|||||)

What i then want to do is to take each entry which is an Array of String and then put into an Array of Array of String.

I get, which is fine:

scala> val current_line_values_1 = lines(3).split("\\|", -1).map{x => val trimmed = x.trim; if (trimmed.isEmpty) "empty string" else trimmed}
current_line_values_1: Array[String] = Array(empty string, 201710, 201711, 201712, empty string, empty string, empty string)

scala> val current_line_values_2 = lines(3).split("\\|", -1).map{x => val trimmed = x.trim; if (trimmed.isEmpty) "empty string" else trimmed}
current_line_values_2: Array[String] = Array(empty string, 201710, 201711, 201712, empty string, empty string, empty string)

Done this way for simplicity in SBT Console.

So, I looked at ListBuffer, because I want to make a List of these 2 which are a subset of all the lines.

I did as per the mutable.ListBuffer using Alvin Alexander's example the following:

var fruits = new ListBuffer[String]()
//var fruits = new ListBuffer[Array[String]]()  - this made no difference either

fruits += current_line_values_1.toString
fruits += current_line_values_2.toString

I had to use toString to get it to compile.

I get the right number of elements, but printing out like this:

      saveLinesList1 = fruits.toList

      // How to print 2 dimensional?
      for (j <- 0 to (saveLinesList1.length - 1)) {

        println("hello " + saveLinesList1(j))
        val x = saveLinesList1(j)
           for (k <- 0 to (x.length - 1)) {
             println("hello 2 " + x(k))
           }

      } // End of for loop.

gives me:

hello [Ljava.lang.String;@51dd09f
hello 2 [
hello 2 L
hello 2 j
hello 2 a
hello 2 v
hello 2 a

What has happened to the content? Array of Array[String] did not help either. Looking at 2 dimensional array did not clear it up either.

All I need is an array of the lines which are array of String.


Solution

  • When you apply toString to an Array it will return back object not the String value. Instead try using mkString which concatenates each value inside Array and returns a single String value. Try using below code,

    //no need to define it as var its already mutable
    val fruits = new ListBuffer[String]()
    fruits += current_line_values_1.mkString
    fruits += current_line_values_2.mkString
    //to print the ListBuffer
    fruits.foreach(println(_))
    

    If you need Array[String] instead of String you can use following,

    val fruits = new ListBuffer[Array[String]]()
    fruits += current_line_values_1
    fruits += current_line_values_2