java杂知识点

java小知识点
1、将一个整数转化为字符串,并求整数的位数。
String.valueOf()的用法:https://blog.csdn.net/u014252478/article/details/82423458
length():用于获取字符串的长度。

public class p3 {
    public static void main(String[] args) {
        int a = 52464;
        String s = String.valueOf(a);
        System.out.println(s.length());
    }
}

输出结果:5

length属性:用于获取数组长度

2、装箱、拆箱操作发生在:引用类型与值类型之间
基本数据类型转化成包装类是装箱 (如: int --> Integer)。
包装类转化成基本数据类型就是拆箱 (如:Integer --> int)。
包装类就是引用类型,基本数据类型就是值类型。

3、Java 提供了一个特别的三元运算符(也叫三目运算符)经常用于取代某个类型的 if-then-else 语句。条件运算符的符号表示为**“?:”**,使用该运算符时需要有三个操作数,因此称其为三目运算符。格式如下:
result = ? : ;
其中,expression 是一个布尔表达式。当 expression 为真时,执行 statement1否则就执行 statement3。此三元运算符要求返回一个结果,因此要实现简单的二分支程序,即可使用该条件运算符。

4、从键盘输入一行字符,挨个打印出来

import java.util.*;
public class p3 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String n = s.nextLine();
        char[] ch = n.toCharArray();
        for (int i = 0; i < n.length(); i++) {
            System.out.print(ch[i]);
        }
    }
}

5、判断字符串中是否是数字:Character.isDigit(str2[i].charAt(2));若为true,则是数字。
6、将字符串中的数字转化为数字:Integer.valueOf(str2[i].substring(1));
7、将数组进行排序:Arrays.sort(str);str是字符串数组
8、字符串和字符串数组的遍历
str=“hsuahu hhgg“;
(1)字符串

for(int i = 0;i<str.length();i++){
		System.out.println(str.charAt(i));
}

(2)字符串数组

String[] str1 = str.split(" ");
for(int i=str1.length-1;i>=0;i--){
            System.out.print(str1[i]+" ");
 }

输入输出

从键盘输入:[[3,5,6],[5,7,8]]

package pactice2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

class Solution {
    public void myFunc(ArrayList<Integer> arr) {
        // 使用自测数据按钮时调试用,正式提交时要删掉。
        //System.out.println(arr);
        Iterator<Integer> iterator = arr.iterator();
        while (iterator.hasNext()){
            Integer i = iterator.next();
            System.out.print(i+" ");
        }
    }
}
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        ArrayList<Integer> row = new ArrayList<Integer>();
        while(cin.hasNextLine())
        {
            //ArrayList<Integer> row = new ArrayList<Integer>();
            String line = cin.nextLine().trim();
            if (line.length() > 0) {
                String[] arrLine = line.replace("]", "").replace(" ", "")
                        .replace("[", "").replace("]", "").split(",");
                for (int i=0; i<arrLine.length; i++) {
                    row.add(Integer.valueOf(arrLine[i]));
                }
            }
        }
        new Solution().myFunc(row);
    }
}

结果:这是单个输入(将while(cin.hasNextLine())去掉),加上时,表示不止一个输入
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值