Comparison of basic operations between java and scala arrays and collections

  • 2021-11-24 01:37:43
  • OfStack

Directory java and scala Array and Set Operation scala Array Basic Operation Corresponding java Code scala Variable Array ArrayBufferjava Corresponding ArrayList Operation scala Array Traversal java Array Traversal scala Array and java Array Comparison Source Code

Operation of java and scala Arrays and Sets

This blog introduces the basic use of scala Arrays + Variable Arrays and the difference from java Arrays

scala Array Basic Operations


def main(args: Array[String]): Unit = {
 
    //new1 Arrays of constant size 
    val nums = new Array[Int](10) // Will be initialized to 0
 
    val s = Array("hello", "world") // Initialize the array with two strings (remember, you don't need it here new ) 
 
    s(0) = "good bye" // Use () To access the array 
 
}

The underlying implementation of scala array is java array, and the above underlying implementation is java. lang. String []

Corresponding java code


public static void main(String[] args) {
    int[] nums = new int[10];
 
    String[] s = new String[]{"hello", "world"};
 
    s[0] = "goodbye";
}

scala Variable Array ArrayBuffer


def main(args: Array[String]): Unit = {
    val b = ArrayBuffer[Int]() // Initialization 
    // Or 
    //    val b = new ArrayBuffer[Int]
 
    b += 1 // Add 1 Elements 
    b += (1, 2, 3, 4, 5) // Add multiple elements at the end 
 
    println(s"b:$b") //b:ArrayBuffer(1, 1, 2, 3, 4, 5)
 
    b ++= Array(8, 1, 34) //1 Secondary addition 1 Array, notice that this is an array 
    println(s"b:$b") //b:ArrayBuffer(1, 1, 2, 3, 4, 5, 8, 1, 34)
 
    b.trimEnd(3) // Remove the last 3 Elements 
    println(s"b:$b") //b:ArrayBuffer(1, 1, 2, 3, 4, 5)
}

java corresponds to ArrayList operation


public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();
    list.add(1);
 
    List<Integer> list2 = new ArrayList<>();
    list2.add(1);
    list2.add(2);
    list2.add(3);
    list2.add(4);
    list2.add(5);
    list.addAll(list2);
    //java1 Secondary addition 1 , 2 , 3 , 4 , 5 It will be a lot of trouble 
    //  Of course use guava Adj. Lists.newArrayList It seems that the code will be simpler 
    //  Or use the following Arrays.asList It will be simpler, and the main reason is that it is directly constructed 1 Containing multiple numbers list Native java Bad support 
 
    System.out.println(list); //[1, 1, 2, 3, 4, 5]
 
    list.addAll(Arrays.asList(1, 2, 3, 4, 5));
    System.out.println(list); //[1, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
 
    //java Native is not provided, remove the last 5 Function of 1 element 
 
    list.add(1,6); //List1 You can only add this time 1 Elements 
    System.out.println(list); //[1, 6, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
 
    list.remove(1);
    System.out.println(list); //[1, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
 
    //list Also does not support, remove the index Continuity on n Number 
 
    Object[] array = list.toArray();// Rotation array 
    Arrays.asList(array); // Turn list
}

Array traversal of scala


def main(args: Array[String]): Unit = {
  val a = Array(1, 2, 3, 4, 5)
  for (i <- 0 until a.length) {
    println(s"i:$i value:${a(i)}")
  }
 
  println("-----------")
 
  // Ergodic interval 1 A 
  for (i <- 0 until a.length by 2) {
    println(s"i:$i value:${a(i)}")
  }
 
  println("-----------")
 
  // Backward traversal 
  for (i <- 0 until a.length reverse) {
    println(s"i:$i value:${a(i)}")
  }
 
  println("-----------")
 
  // If you don't need it index
  for (ele <- a) {
    println(s"value:${ele}")
  }
}

Traversal of java Array


public static void main(String[] args) {
    int[] a = new int[]{1, 2, 3, 4, 5};
 
    for (int i = 0; i < a.length; i++) {
        System.out.println("index:" + i + ",value:" + a[i]);
    }
 
    System.out.println("-----------------");
 
    // Traversing through 2
    for (int i = 0; i < a.length; i += 2) {
        System.out.println("index:" + i + ",value:" + a[i]);
    }
 
    System.out.println("-----------------");
 
    // Backward traversal 
    for (int i = a.length - 1; i >= 0; i--) {
        System.out.println("index:" + i + ",value:" + a[i]);
    }
 
    System.out.println("-----------------");
 
    // Don't care index
    for (int value : a) {
        System.out.println("value:" + value);
    }
}

The traversal of java array is similar to that of scala array. One point that needs to be mentioned is that the traversal mode of scala is compared with unification 1. Whether it is Array or ArrayBuffer, list and array of java are not similar (array uses [], get uses get (), and scala is ())

By comparison, the scala provides more powerful and easy-to-use variable arrays than the java.

Comparison between scala Array and java Array

java array is not a class, scala array is a class

java Definition


int[] a = new int[]{1, 2, 5};

scala definition, scala, which is actually a syntax sugar, calls the apply method


val a=Array(1,2,5)

On source code

scala


final class Array[T](_length: Int) extends java.io.Serializable with java.lang.Cloneable {
  /** The length of the array */
  def length: Int = throw new Error()
  // The element at given index.
  def apply(i: Int): T = throw new Error()
  // Update the element at given index.
  def update(i: Int, x: T) { throw new Error() }
  //Clone the Array.
  override def clone(): Array[T] = throw new Error()
}

For different generic T, scala has different implementations, such as Int


 /** Creates an array of `Int` objects */
  // Subject to a compiler optimization in Cleanup, see above.
  def apply(x: Int, xs: Int*): Array[Int] = {
    val array = new Array[Int](xs.length + 1)
    array(0) = x
    var i = 1
    for (x <- xs.iterator) { array(i) = x; i += 1 }
    array
  }

java source code

java can not find the source code of Array ~!


Related articles: