ArrayBuffer or 3D list Array


#1

Hi All,

Can i have as below? As one array (dimension is fixed) and have a ArrayBuffer to hold all instances of each row.

var a = Array( “Field”, “OField”,Array(“a”, “b”, “c”), Array(“d”, “e”, “f”, “g”, “h”) )

list+=Array( “Field1”, “OField1”,Array(“a1”, “b1”, “c1”), Array(“d1”, “e1”, “f1”, “g1”, “h1”) )
list+=Array( “Field2”, “OField2”,Array(“a2”, “b2”, “c2”), Array(“d2”, “e2”, “f2”, “g2”, “h2”) )

println(list(0)(2)(0)) —> will print a

Kindly suggest. How to initialize, add and retrieve values with the right collection type.
Can anyone please suggest.

I am trying to create a string array. It should incremental like a ArrayBuffer or ListBuffer as the dim is not fixed. But not able to decide which collection exactly i should be going with.
Each row needs to be at (0) index followed by 2nd, 3rd and 4th columns divided among SUBARRY1 and SUBARRAY2.

Example: Below structure should be for each of the rows.
OFIELD_NAME --> ELIG_INDC
OFIELD --> XXX
RCTRYNUM–> ***
BH_LEGACY_CD–> *
RULE_ID–> A1
OVALUE–> N
IFIELD_NAME–> SYS_ID
OP_CD —> EQ
IFIELD --> 04
ect…

image


#2

An Array (or ArrayBuffer) can only contain instances of a single type. Your example of

var a = Array( “Field”, “OField”,Array(“a”, “b”, “c”), Array(“d”, “e”, “f”, “g”, “h”) )

has an array containing both Strings and Arrays. At this level what you will need is a case class. Either

import scala.collection.mutable
case class Row(name: String, oldName: String, columns: mutable.ArrayBuffer[mutable.ArrayBuffer[String]])

val rows = new mutable.ArrayBuffer[Row]()

// population is fiddly, which this margin is too narrow to contain

println( row(0).columns(0)(0) ) // "a"

If possible I would do more work to determine sizes before construction. ArrayBuffers are extremely mutable. Working with fixed sized Arrays would remove a bit of complication and places bugs can hide. Using something like a Vector and further limiting mutability might also be helpful in a long lived program. (If this is write-and-use once code then just stick with ArrayBuffers.)


#3

Thanks Lanny for your suggestion. I do agree with you.
I in fact tried with Map inside a ArrayBuffer and seems to be working for me. And also tried with simple design to hold all of them as string and convert them when ever required.

var arrFIELD_REC=new ArrayBufferMap[String, String]