首页 > 其他分享 >Arrays类

Arrays类

时间:2024-04-21 17:33:35浏览次数:13  
标签:Arrays System int toString print out

import java.util.Arrays;

public class Demo05 {
public static void main(String[] args) {
int []a={111,2,5,6,999,7777,2,33,};

    //打印数组元素--Arrays.toString
    //System.out.println(Arrays.toString(a));
    //printArray(a);

    Arrays.sort(a);//数组进行排序
    System.out.println(Arrays.toString(a));
}

//自己定义打印数组元素的输出样式
public static void printArray(int []a){
for (int i = 0; i<a.length; i++) {
if (i==0){
System.out.print("[");

        }
        if (i==a.length-1){
            System.out.print (a[i]+"]");
        }
        else{
            System.out.print(a[i]+", ");
        }


    }
}

}

标签:Arrays,System,int,toString,print,out
From: https://www.cnblogs.com/anonymity12/p/18149226

相关文章

  • CF1165E Two Arrays and Sum of Functions 题解
    题目简述已知两个长度均为$n$的数组$a$和$b$。给定一个函数:$f(l,r)=\sum\limits_{l\lei\ler}a_i\cdotb_i$。你的任务是对数组$b$中的元素以任意的顺序重新排序,使$\sum\limits_{1\lel\ler\len}f(l,r)$的值最小。题目分析我们首先进行化简,发现题......
  • Java入门基础知识第八课(数组)——冒泡排序、Arrays工具类
    前面二白讲了关于数组的概念、语法以及简单的输入输出,实际上关于数组的知识还有很多,接下来咱们讲一下冒泡排序以及一些常用的Arrays工具类,需要记忆的知识很多,而且容易混淆。一、冒泡排序简介(原理)升序为例:从头开始,每次比较相邻两数小的交换到前面每轮结束后最大的数交换到......
  • Java中 Arrays.asList() 处理基本数据类型数组和包装类型数组的差异
    文章目录前言一、基本数据类型数组和包装类型数组的区别二、Arrays.asList()对这两种数组的处理方式总结前言在使用Java中的Arrays.asList()方法时,我们可能会发现它对基本数据类型数组和包装类型数组的处理方式存在一些差异。这种差异体现了Java在基本数据......
  • LeetCode 2461. Maximum Sum of Distinct Subarrays With Length K
    原题链接在这里:https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/description/题目:Youaregivenanintegerarray nums andaninteger k.Findthemaximumsubarraysumofallthesubarraysof nums thatmeetthefollowingconditi......
  • 集合:Arrays.asList() 和 new ArraysList()
     Arrays.asList和newArrayList在Java中都与集合有关,但是它们之间有一些重要的区别。概述1)Arrays.asList():T...是一个可变参数(varargs),接收任意参数后,会组合为一个数组。Arrays.asList("字符串列表.");//字符串列表Arrays.asList(2024)......
  • CF1896C Matching Arrays 题解
    题目简述给定两个长度为$n$的数列$a,b$,再给定一个数$x$,请你判断是否存在一种重排$b$数列的方式,使得满足$a_i>b_i$的$i$恰好有$x$个。$n\leq2\times10^5$。题目分析遇到这种可行性问题,首先考虑做出最优解,以此来判断是否无解。接下来,可以思考最优解如何构造,我们......
  • Arrays简单的使用方法
    数组packagemethod;importjava.util.Arrays;publicclassArrayDemo04{publicstaticvoidmain(String[]args){int[]a={1,2,15,47,56,22,222,11,4,4455};//Arrays.sort(a);//排序:升序Arrays.fill(a,2,4,0);//从2-4被0填充,fill填充......
  • Arrays工具类
    5.Arrays工具类5.1介绍数组操作工具类,专门用于操作数组元素方法名说明publicstaticStringtoString(类型[]a)将数组元素拼接为带有格式的字符串publicstaticbooleanequals(类型[]a,类型[]b)比较两个数组内容是否相同publicstaticintbinarySearch(in......
  • ArrayIndexOutOfBoundException and NegativeArraySizeException in Java
    ArrayIndexOutOfBoundExceptionArrayIndexOutOfBoundsException occurswhenweaccessanarray,ora Collection,thatisbackedbyanarraywithaninvalidindex. Thismeansthattheindexiseitherlessthanzeroorgreaterthanorequaltothesizeofthe......
  • java8中,Arrays.sort()默认是升序的,对于基本数据类型,使其降序怎么实现
    对于引用数据类型,自定义比较器对象,实现Comparator接口/Comparable接口对于基本数据类型,自定义比较器对象,将基本数据类型转换成对应的包装类型即可但是这样写是错误的,importjava.util.Arrays;importjava.util.Comparator;publicclassSortExample{publicstatic......