当前位置: 代码迷 >> 综合 >> Scala 遇到问题
  详细解决方案

Scala 遇到问题

热度:49   发布时间:2024-01-05 10:16:38.0

关键字

Scala while遇到问题

# scala -version
Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL
    val result = new ByteArrayOutputStream();var buffer: Array[Byte] = new Array[Byte](1024)var length = inputStream.read(buffer)while (false){
    println("false,false,false")}while ((length = inputStream.read(buffer)) != -1) {
    println("::::: " + (length != -1))println("length =" + length + " | (length != -1) = " + (length != -1))length = inputStream.read(buffer)result.write(buffer, 0, length);}

输出

::::: false
length =-1 | (length != -1) = false
Exception in thread "main" java.lang.IndexOutOfBoundsExceptionat java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:151)at com.bl.bigdata.cdp.cdpstream.MonitorApplication$.send_http_request(MonitorApplication.scala:36)at com.bl.bigdata.cdp.cdpstream.MonitorApplication$.main(MonitorApplication.scala:11)at com.bl.bigdata.cdp.cdpstream.MonitorApplication.main(MonitorApplication.scala)

while ((length = inputStream.read(buffer)) != -1)在scala中没有生效,问题原因还需要再查看.

需要修改为

    val buffer: Array[Byte] = new Array[Byte](1024)var length = inputStream.read(buffer)while (length != -1) {
    println("::::: " + (length != -1))result.write(buffer, 0, length);length = inputStream.read(buffer)}
// ORin.readLine.foreach(x => {
    sb.append(line);sb.append("\n");})

与Java交互

数组

java file


public class Cat {
    private String name = "none";public Cat(String name) {
    this.name = name;}@Overridepublic String toString() {
    return "Cat name is " + name + "";}public String getName() {
    return name;}public void setName(String name) {
    this.name = name;}
}public class CC1 {
    public static void printlnBasicArray(int[] arrs) {
    for (int arr : arrs) {
    System.out.println(arr);}}public static void printlnObjArray(Cat[] arrs) {
    for (Cat arr : arrs) {
    System.out.println(arr);}}
}

打印基础java数组

    val ints = Array(1, 2, 4, 5)CC1.printlnBasicArray(ints)

打印基础java对象数组

    val cats = Array(new Cat("mi1"), new Cat("mi2"))CC1.printlnObjArray(cats)

遇到问题

import java.util.{
    Date, Properties}
import javax.mail.internet.{
    InternetAddress, MimeMessage}
import javax.mail.{
    Message, Session}
// 由于InternetAddress是Address 子类,就直接用了
val address1 = new InternetAddress("xxx1@qq.com")
val address2 = new InternetAddress("xxx2@bl.com")
val addresses = Array(address1, address2)// ...
val msg = new MimeMessage(session);
msg.setRecipients(Message.RecipientType.CC, addresses) //
// ...

输出

C:\src\bl\cdpstream\src\test\scala\MailTest.scala:65:9
overloaded method value setRecipients with alternatives:(x$1: javax.mail.Message.RecipientType,x$2: String)Unit <and>(x$1: javax.mail.Message.RecipientType,x$2: Array[javax.mail.Address])Unitcannot be applied to (javax.mail.Message.RecipientType, Array[javax.mail.internet.InternetAddress])msg.setRecipients(Message.RecipientType.CC, address)

解决方式
输出错误非常显示为类型不一样.

方法一

 val address1 = new Address {
    override def getType: String = "rfc822";override def toString: String = "xxx1@qq.com"override def equals(obj: Any): Boolean = obj.toString.equals(toString)}val address2 = new Address {
    override def getType: String = "rfc822";override def toString: String = "xxx2@bl.com"override def equals(obj: Any): Boolean = obj.toString.equals(toString)}val address = Array(address1, address2)msg.setRecipients(Message.RecipientType.CC, address)

方法二

val address1 = new InternetAddress("xxx1@qq.com")
val address2 = new InternetAddress("xxx2@bl.com")val address = Array(address1.asInstanceOf[Address], address2.asInstanceOf[Address])
msg.setRecipients(Message.RecipientType.CC, address)
  相关解决方案