java与scala数组及集合的基本操作对比
作者:端吉 时间:2022-03-03 05:52:27
java与scala数组及集合的操作
这篇博客介绍了scala的数组 + 可变数组的基本使用,及其与java数组的区别
scala数组基本操作
def main(args: Array[String]): Unit = {
//new一个大小不变的数组
val nums = new Array[Int](10) //会被初始化为0
val s = Array("hello", "world") //使用两个字符串初始化数组(记住,这里不需要new)
s(0) = "good bye" //使用()来访问数组
}
scala数组底层实现就是java数组,上述底层是java.lang.String[]
对应java代码
public static void main(String[] args) {
int[] nums = new int[10];
String[] s = new String[]{"hello", "world"};
s[0] = "goodbye";
}
scala可变数组ArrayBuffer
def main(args: Array[String]): Unit = {
val b = ArrayBuffer[Int]() //初始化
//或者
// val b = new ArrayBuffer[Int]
b += 1 //添加一个元素
b += (1, 2, 3, 4, 5) //添加多个元素在末尾
println(s"b:$b") //b:ArrayBuffer(1, 1, 2, 3, 4, 5)
b ++= Array(8, 1, 34) //一次添加一个数组,注意这里是数组
println(s"b:$b") //b:ArrayBuffer(1, 1, 2, 3, 4, 5, 8, 1, 34)
b.trimEnd(3) //移除最后三个元素
println(s"b:$b") //b:ArrayBuffer(1, 1, 2, 3, 4, 5)
}
java 相应 ArrayList操作
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);
//java一次要添加1,2,3,4,5就麻烦很多了
// 当然使用guava的Lists.newArrayList看起来代码会简单些
// 或者使用下面的Arrays.asList也会简单些,最主要的原因是直接构造一个含有多个数字的list原生的java支持的不好
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没有提供原生的,移除最后5个元素的函数
list.add(1,6); //List一次只能添加一个元素
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也不支持,移除第index上的连续n个数字
Object[] array = list.toArray();//转数组
Arrays.asList(array); //转list
}
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("-----------")
//遍历隔一个
for (i <- 0 until a.length by 2) {
println(s"i:$i value:${a(i)}")
}
println("-----------")
//倒着遍历
for (i <- 0 until a.length reverse) {
println(s"i:$i value:${a(i)}")
}
println("-----------")
//如果不需要index
for (ele <- a) {
println(s"value:${ele}")
}
}
java数组的遍历
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("-----------------");
//遍历隔着2
for (int i = 0; i < a.length; i += 2) {
System.out.println("index:" + i + ",value:" + a[i]);
}
System.out.println("-----------------");
//倒着遍历
for (int i = a.length - 1; i >= 0; i--) {
System.out.println("index:" + i + ",value:" + a[i]);
}
System.out.println("-----------------");
//不关心index
for (int value : a) {
System.out.println("value:" + value);
}
}
java数组和scala数组遍历差不多,需要提的一点是,scala的遍历方式比较统一,无论是Array还是ArrayBuffer,java的list和array就不大一样(array使用[]取值,list使用get()取值,而scala都是())
对比下就知道,scala提供的可变数组比java的更加强大易用。
scala数组与java数组对比
java数组不是个类,scala数组是类
java定义
int[] a = new int[]{1, 2, 5};
scala定义,scala这个实际是个语法糖,调用了apply方法
val a=Array(1,2,5)
源码上
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()
}
针对不同的泛型T,scala有不同的实现,比如针对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 源码
java 找不到Array的源码~!
来源:https://www.jianshu.com/p/2914a94b8022
标签:java,scala数组,集合
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Springboot文件上传功能的实现
2021-09-20 12:48:47
![](https://img.aspxhome.com/file/2023/0/64400_0s.png)
三步轻松搭建springMVC框架
2023-02-01 05:38:34
![](https://img.aspxhome.com/file/2023/6/64036_0s.jpg)
Spring boot如何集成kaptcha并生成验证码
2023-09-13 04:00:24
![](https://img.aspxhome.com/file/2023/8/64918_0s.png)
Spring依赖注入的三种方式小结
2022-08-09 15:56:41
![](https://img.aspxhome.com/file/2023/1/62641_0s.png)
Spring Boot集成ElasticSearch实现搜索引擎的示例
2021-06-02 05:06:16
![](https://img.aspxhome.com/file/2023/3/63793_0s.png)
java中Object类4种方法详细介绍
2023-11-03 16:06:12
![](https://img.aspxhome.com/file/2023/4/58824_0s.jpg)
Java可变个数形参的方法实例代码
2023-01-15 18:35:56
![](https://img.aspxhome.com/file/2023/2/62372_0s.png)
Flutter 状态管理的实现
2023-08-21 02:38:33
![](https://img.aspxhome.com/file/2023/0/108330_0s.png)
Java Thread之Sleep()使用方法及总结
2023-11-16 10:38:35
![](https://img.aspxhome.com/file/2023/4/59014_0s.png)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer异常
2022-07-03 11:12:34
java写入文件的几种方法分享
2023-06-26 15:56:55
Java8内存模型PermGen Metaspace实例解析
2023-11-25 10:53:36
![](https://img.aspxhome.com/file/2023/8/59968_0s.jpg)
浅析Java内存模型与垃圾回收
2023-11-23 06:11:58
![](https://img.aspxhome.com/file/2023/5/59965_0s.png)
Java递归来实现汉诺塔游戏,注释详细
2023-07-05 10:33:52
![](https://img.aspxhome.com/file/2023/2/64412_0s.jpg)
SpringBoot项目从搭建到发布一条龙
2023-11-21 09:28:44
![](https://img.aspxhome.com/file/2023/7/58847_0s.png)
C++高并发内存池的整体设计和实现思路
2023-07-03 16:29:31
![](https://img.aspxhome.com/file/2023/3/103083_0s.png)
永久解决idea git log乱码的问题
2022-01-10 06:13:13
![](https://img.aspxhome.com/file/2023/4/63434_0s.jpg)
Java 数据结构与算法系列精讲之字符串暴力匹配
2021-10-06 05:43:27
![](https://img.aspxhome.com/file/2023/5/65305_0s.jpg)
Jmeter分布式压力测试实现过程详解
2022-03-23 21:32:44
![](https://img.aspxhome.com/file/2023/0/63040_0s.png)
SpringBoot之通过BeanPostProcessor动态注入ID生成器案例详解
2023-11-24 22:17:26
![](https://img.aspxhome.com/file/2023/1/59251_0s.jpg)