Java输出+数组

1 篇文章 0 订阅
1 篇文章 0 订阅

Java输出

java要调用到util这个包
利用scanner进行输出。

import java.util.Scanner;
public class TestScanner {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
System.out.println("Result is:"+a);
	}

}

输入:5,则结果输出为:“Result is:5”
在这里插入图片描述
如是输入字符串则变成String a = scanner.next();
这样的字符串的输入只能一个字符,因为System.in。如果要得到一行字符,那么可以用BufferedReader。将System.in封装成InputStreamReader,可以获得更多功能,其中readLine()可以读取一行。

public class TestScanner {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
System.out.println(s);
int n = Integer.parseInt( s);
System.out.println(n);
	}

}

在这里插入图片描述

对数组进行遍历

for(int Array:Arrays)。

int[] arrays = new int[5];
       for(int array:arrays){
           System.out .println(array);
       }

在这里插入图片描述
另有一种:System.out.println(Array.toString(array));

int[] arrays = new int[5];
System.out.println(Array.toString(array));

这样输出的就是[0,0,0,0,0]。

对数组进行复制

system.arraycopy(source,0,det,0,5)对数组进行复制操作,就可以不用for循环进行了。

int[] heres = new int[]{1,5,9,56,22,15,85};
       int[] theres = new int[5];
        System.arraycopy(heres,0,theres,0,5);
        for(int there:theres){
            System.out.println(there);
        }

其中: System.arraycopy(heres,0,theres,0,5);
里面heres是源数组;
0是从索引第几位开始复制;
theres是要复制过去的新数组空间;
0是复制到索引第几位;
5是要复制的长度。
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值