Java数组技巧攻略
Java数组技巧攻略
0. 声明一个数组(Declare an array)
- String[] aArray = new String[5];
- String[] bArray = {"a","b","c", "d", "e"};
- String[] cArray = new String[]{"a","b","c","d","e"};
String[] aArray = new String[5];
String[] bArray = {"a","b","c", "d", "e"};
String[] cArray = new String[]{"a","b","c","d","e"};
1. 在Java中输出一个数组(Print an array in Java)
- int[] intArray = { 1, 2, 3, 4, 5 };
- String intArrayString = Arrays.toString(intArray);
- // print directly will print reference value
- System.out.println(intArray);
- // [I@7150bd4d
- System.out.println(intArrayString);
- // [1, 2, 3, 4, 5]
int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray); // print directly will print reference value
System.out.println(intArray);
// [I@7150bd4d System.out.println(intArrayString);
// [1, 2, 3, 4, 5]
2. 从数组中创建数组列表(Create an ArrayList from an array)
- String[] stringArray = { "a", "b", "c", "d", "e" };
- ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
- System.out.println(arrayList);
- // [a, b, c, d, e]
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
System.out.println(arrayList);
// [a, b, c, d, e]
3. 检查数组中是否包含特定值(Check if an array contains a certain value)
- String[] stringArray = { "a", "b", "c", "d", "e" };
- boolean b = Arrays.asList(stringArray).contains("a");
- System.out.println(b);
- // true
String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true
4. 连接两个数组( Concatenate two arrays)
- int[] intArray = { 1, 2, 3, 4, 5 };
- int[] intArray2 = { 6, 7, 8, 9, 10 };
- // Apache Commons Lang library
- int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
5. 声明一个数组内链(Declare an array inline )
- method(new String[]{"a", "b", "c", "d", "e"});
method(new String[]{"a", "b", "c", "d", "e"});
6. 将数组元素加入到一个独立的字符串中(Joins the elements of the provided array into a single String)
- // containing the provided list of elements
- // Apache common lang
- String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
- System.out.println(j);
- // a, b, c
// containing the provided list of elements
// Apache common lang
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j);
// a, b, c
7. 将数组列表转换成一个数组 (Covnert an ArrayList to an array)
- String[] stringArray = { "a", "b", "c", "d", "e" };
- ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
- String[] stringArr = new String[arrayList.size()];
- arrayList.toArray(stringArr);
- for (String s : stringArr)
- System.out.println(s);
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
8. 将数组转换成一个集合(Convert an array to a set)
- Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
- System.out.println(set);
- //[d, e, b, c, a]
Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);
//[d, e, b, c, a]
9. 反向数组(Reverse an array)
- int[] intArray = { 1, 2, 3, 4, 5 };
- ArrayUtils.reverse(intArray);
- System.out.println(Arrays.toString(intArray));
- //[5, 4, 3, 2, 1]
int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]
10. 删除数组元素(Remove element of an array)
- int[] intArray = { 1, 2, 3, 4, 5 };
- int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
- System.out.println(Arrays.toString(removed));
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));
One more – convert int to byte array
- byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
- for (byte t : bytes) {
- System.out.format("0x%x ", t);
- }
Java数组技巧攻略的更多相关文章
- 直接拿来用!超实用的Java数组技巧攻略
java编程语言经验分享 摘要:本文分享了关于Java数组最顶级的11大方法,帮助你解决工作流程问题,无论是运用在团队环境或是在私人项目中,你都可以直接拿来用. 本文分享了关于Java数组最顶级的11 ...
- 直接拿来用!超实用的Java数组技巧攻略[转]
来自csdn http://www.csdn.net/article/2013-09-16/2816947-methods-for-java-arrays 本文分享了关于Java数组最顶级的11大方法 ...
- 超实用的Java数组技巧攻略分享!
本文分享了关于Java数组最顶级的11大方法,帮助你解决工作流程问题,无论是运用在团队环境或是在私人项目中,你都可以直接拿来用! 声明一个数组(Declare an array) String[] a ...
- Java代理全攻略【有瑕疵:字节码生成部分没看到,最后两节没仔细看,累了】
Java代理 1.代理模式 定义:给某个对象提供一个代理对象,并由代理对象控制对于原对象的访问,即客户不直接操控原对象,而是通过代理对象间接地操控原对象. 其实就是委托.聚合.中间人. 为了保持行为的 ...
- Java学习全攻略-->阅读官方文档
一直感觉Java的官方文档有些杂乱,最近特意整理了一下,仅供参考. 入口 Oracle官方文档入口:http://docs.oracle.com/.下级页面这边只整理了JavaEE跟JavaSE的文档 ...
- 如何优雅地使用containerd?这里有一份必读的技巧攻略
前 言 Docker是我们常用的容器runtime,友好的CLI,丰富的社区资料,外加研发运维人员多年的经验积累,使用Docker几乎是没有任何门槛的事.而k3s为了降低资源消耗,将默认的runtim ...
- 嘿,java打怪升级攻略
Java成神之路 第一层 java基础 **当你通过本层所有关卡,你可以完成一些简单的管理系统.坦克大战游戏.QQ通信等. ** 第二层 数据库 数据库类型很多例如:MySQL.oracle.redi ...
- 8个超实用的jQuery技巧攻略
1)禁用右键单击功能 如果你想为用户节省网站信息,那么开发者可以使用这段代码——禁用右键单击功能. <font><font>$(document).ready(function ...
- 关于Java高并发编程你需要知道的“升段攻略”
关于Java高并发编程你需要知道的"升段攻略" 基础 Thread对象调用start()方法包含的步骤 通过jvm告诉操作系统创建Thread 操作系统开辟内存并使用Windows ...
随机推荐
- spoj 371 Boxes
N个盒子围成一圈,第i个盒子初始时有Ai个小球,每次可以把一个小球从一个盒子移到相邻的两个盒子之一里.问最少移动多少次使得每个盒子中小球的个数不超过1. ΣAi<=N.1<=N<=1 ...
- miniui后台无法接收到input传值
出错原因:在miniui中,此处应写成<input textName="current_unit",在php中才可以使用$_POST['current_unit']获取到值, ...
- [Android]Volley源码分析(四)
上篇中有提到NetworkDispatcher是通过mNetwork(Network类型)来进行网络访问的,现在来看一下关于Network是如何进行网络访问的. Network部分的类图:
- BZOJ4570: [Scoi2016]妖怪
题目传送门 4570: [Scoi2016]妖怪 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 491 Solved: 125 [Submit][Sta ...
- c#.Net:Excel导入/导出之NPOI 2.0简介
NPOI 2.0+主要由SS, HPSF, DDF, HSSF, XWPF, XSSF, OpenXml4Net, OpenXmlFormats组成,具体列表如下: 资料来自:百度百科 Ass ...
- 精简高效的css命名准则
对于css,为了避免样式冲突,我们总会赋予相当特殊的命名,或是在选择符上添加html标记,或是使用层级.我们为避免在冲突上做文章,就会把代码的命名变得复杂化. 如果css的重用性越高,相比就越高效.如 ...
- coredump简介与coredump原因总结
from:http://www.cnblogs.com/doctorqbw/archive/2011/12/21/2295962.html 千兵卫博士 coredump简介与coredump原 ...
- 高效的jQuery
选择捷径 // 糟糕 if(collection.length > 0){..} // 建议 if(collection.length){..} 熟记技巧 // 糟糕 $('#id').data ...
- unix编程书中的 ourhdr.h代码
真心不知到里面写的具体什么意思,先记下吧. /*Our own header, to be included after all standard system headers*/ #ifndef _ ...
- [IOS基础]关于IOS的UIScreeen,UIView,UIViewController,UIWindow理解
UIScreen: 代表当前这个屏幕,通过UIApplication可以获得这个属性 UIView: 一个矩形试图,包含用户手势和时间响应 UIViewController: 一个UIView的集 ...