如何配置 Scala 表中的列名? [英] How do I configure the Column names in a Scala Table?

查看:42
本文介绍了如何配置 Scala 表中的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Scala 程序来管理数据库,并将所有数据绘制到一个二维 ArrayBuffer 中,其中第 0 行是列名,随后的行包含表中每个条目的信息.尝试将其放入表格时,我该如何分配列标题?

I am writing a Scala program to manage a database, and have drawn all of the data into a 2-dimensional ArrayBuffer where row 0 is the column names, and the subsequent rows contain the info for each entry in the table. When trying to put this into a Table, ho=w do I go about assigning the Column headers?

非常感谢语法建议.

伪代码:

Data=ArrayBuffer()
Data(0)={"Name","Birthday","ID"}
Data(1)={"Bob", "07/19/1986", "2354"}
Data(2)={"Sue", "05/07/1980", "2355"}
Data(3)={"Joe", "08/12/1992", "2356"}
Data(4)={"Jim", "11/20/1983", "2357"}

我想把它放到一个表中,其中 Data(0) 描述列标题,随后的行描述表中的行,但我不知道如何设置行标题.

I want to put this into a Table where Data(0) describes the column headers, and the subsequent rows describe rows in the table, but I can't figure out how to set the row headers.

推荐答案

将数据放入 Table 的最简单方法是使用其构造函数:

The easiest way to put data in a Table is to use its constructor:

new Table (rowData: Array[Array[Any]], columnNames: Seq[_]) 

这里有点棘手的是数组不是协变的(参见 为什么不协变示例编译,也就是(co-、contra- 和 in-)方差如何工作?),这意味着 Array[String]not Array[Any] 的子类型.所以你需要某种方式把一个变成另一个:一个 map 来完成这项工作.

The slightly tricky thing here is that arrays are not covariant (see Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work?), which means that an Array[String] is not a subtype of Array[Any]. So you need some way of turning one into the other: a map does the job.

此外,要显示列名,您需要将表格放入 ScrollPane.

Also, for the column names to show, you need to put the table in a ScrollPane.

import swing._
import collection.mutable.ArrayBuffer

object Demo extends SimpleSwingApplication {

  val data = ArrayBuffer(
    Array("Name","Birthday","ID"),
    Array("Bob", "07/19/1986", "2354"),
    Array("Sue", "05/07/1980", "2355")
  )

  def top = new MainFrame {
    contents = new ScrollPane {
      contents = new Table(
        data.tail.toArray map (_.toArray[Any]),
        data.head
      )
    }
  }
}

会给你一张桌子:

您也可以使用强制转换:data.tail.toArray.asInstanceOf[Array[Array[Any]]],这比映射更有效.

you can also use a cast: data.tail.toArray.asInstanceOf[Array[Array[Any]]], which is more efficient than mapping.

这篇关于如何配置 Scala 表中的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆