0

So I have some data:

val data = Array[(Array[String], Long)]

Which is an array of pairs, where each pair consists of another array and a number. I have created a ListBuffer as follows:

var x = ListBuffer[(Array[String], Long)]

I would like to append an element at some index i to x. I do the following:

 x += data(i)

However, I get the error:

Type mismatch, expected: String, actual: (Array[String], Long)

What am I doing wrong? I am working in IntelliJ and Scala 2.10.7.

Also this example (which is a concrete example (I cannot provide the original example)) also gives the same error:

val xData = ListBuffer[(Array[String], Long)]
    var x = ListBuffer[(Array[String], Long)]
    x += xData(0)
2
  • Could you please provide a complete example? Commented Dec 7, 2017 at 11:02
  • I added a different example, but same error, it is concrete Commented Dec 7, 2017 at 11:30

2 Answers 2

1

I have tried the same thing in scala REPL just now, and it is working fine.

What I did is -

    scala> val arr = Array("Hello", "Bye")
    arr: Array[String] = Array(Hello, Bye)

    scala> var data = Array((arr,1), (arr,2))
    data: Array[(Array[String], Int)] = Array((Array(Hello, Bye),1), 
    (Array(Hello, Bye),2))

    scala> import scala.collection.mutable.ListBuffer
    import scala.collection.mutable.ListBuffer


    scala> val list = ListBuffer((arr, 1))
    list: scala.collection.mutable.ListBuffer[(Array[String], Int)] = 
    ListBuffer((Array(Hello, Bye),1))


    scala> list += data(0)
    res1: list.type = ListBuffer((Array(Hello, Bye),1), (Array(Hello, 
    Bye),1))

It did not give any error

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, it's odd, can it be because I never specified how Array[String] should be?
I cant make an array and use it to create a ListBuffer, it says it doesnt know what the array is. So even your example is broken
There must be some problem in the way you are creating the variables.
0

So the issue was that I forgot brackets at the end of ListBuffer initiation.

This works:

var x = ListBuffer[(Array[String], Long)]()

3 Comments

you can't assign dataTypes to variables?
What do you mean?
ListBuffer[(Array[String], Long)] is definition of datatypes and you can't assign datatypes as you did. you can define datatypes using : as var x: ListBuffer[(Array[String], Long)] = actual value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.