Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////
- // A.scala //
- /////////////
- object A extends App {
- def acceptArray(xs: Array[Double]): Unit = {
- println(s"xs = ${xs.toBuffer}")
- }
- def acceptBuffer(xs: mutable.Buffer[Double]): Unit = {
- println(s"xs = $xs")
- }
- def acceptImmutableList(xs: List[Double]): Unit = {
- println(s"xs = $xs")
- }
- }
- /////////////
- // AT.java //
- /////////////
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.List;
- import scala.collection.JavaConverters;
- /** A {@code AT} is an object that... */
- public class AT {
- public static void main(String[] args) {
- List<Double> doubles = Arrays.asList(1.0, 2.0, 3.0);
- // Buffer<Double> xs = JavaConverters.asScalaBuffer(doubles);
- A.acceptArray(toPrimitive(doubles));
- A.acceptBuffer(scala.collection.JavaConverters.asScalaBuffer((List) doubles)); // more efficient bug...
- A.acceptBuffer(scala.collection.JavaConverters.asScalaBuffer(new ArrayList<>(doubles)));
- scala.collection.immutable.List<Object> scalaList =
- JavaConverters.asScalaIterator((Iterator) doubles.iterator()).toList();
- A.acceptImmutableList(scalaList);
- }
- public static double[] toPrimitive(final List<Double> list) {
- int n = list.size();
- if (n == 0) {
- return new double[0];
- }
- final double[] result = new double[n];
- for (int i = 0; i < n; i++) {
- result[i] = list.get(i);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement