新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java中数组的使用方法-创新互联

这篇文章运用了实例代码展示java中数组的使用方法,代码非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

成都创新互联2013年至今,是专业互联网技术服务公司,拥有项目成都网站设计、网站建设、外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元太和做网站,已为上家服务,为太和各地企业和个人服务,联系电话:028-86922220

创建一个数组:

String[] a = new String[5];
String[] b = {“a”,”b”,”c”, “d”, “e”};
String[] c = new String[]{“a”,”b”,”c”,”d”,”e”};

1、打印数组

我们经常使用for循环或者一些迭代器来打印出数组的所有元素,但我们也可以换个姿势。

int array[] = {1,2,3,4,5};
System.out.println(array); //[I@1234be4e
String arrStr = Arrays.toString(array);
System.out.println(array); //[1,2,3,4,5];

2、创建ArrayList

String[] array = { “a”, “b”, “c”, “d”, “e” };
ArrayList arrayList = 
                        new ArrayList(Arrays.asList(array));
System.out.println(arrayList);
// [a, b, c, d, e]

3、检查是否包含某个值

int array[] = {1,2,3,4,5};
boolean isContain= Arrays.asList(array).contains(5);
System.out.println(isContain);
// true

4、连接两个数组

int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(array1, array2);

5、在一行声明一个数组

method(new String[]{"a", "b", "c", "d", "e"});

6、数组倒置

int[] intArray = { 1, 2, 3, 4, 5 };
// Apache Commons Lang library
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]

7、删除某个元素

int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));

8、转化为set

Set set = new HashSet(Arrays.asList(new String[]{"a", "b", "c", "d", "e"}));
System.out.println(set);
//[d, e, b, c, a]

9、将Array List转化为Array

String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList arrayList = new ArrayList(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);

10、将数组元素组成一个字符串

// Apache common lang
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j); //a, b, c

关于java中数组的使用方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


当前名称:java中数组的使用方法-创新互联
网站URL:http://www.wjwzjz.com/article/cdssjc.html
在线咨询
服务热线
服务热线:028-86922220
TOP