[JAVA]标准输入流笔记1

今天在做阿里巴巴笔试题时,他的输入是两行数据,第一行是两个数,应该放入一个数组里面,第二行是一行整数,第一个数字赋值给一个int型变量, 后面的数据个数和第一个数相关,比如4倍的关系。主要是什么时候输入结束,才能把相应个数的数输入数组中。对这方面真的不是很了解,平时感觉很小的问题到了考试的时候才发现一脸懵逼,所以感觉还是多做笔记,不要以为不起眼就不好好弄,一定要懂得细节决定成败,多注意基础 的学习,不要好高骛远。。。扯远了,直接上代码吧,只是总结,后续还会有更新。

package book_datastruction;

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

public class TestSystemIn {

    static int a0;//自动初始化为0
    int a_1;//自动初始化为0
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a1 = 0 ;
        TestSystemIn ts = new TestSystemIn();
        System.out.println("ts.a_1="+ts.a_1);
        System.out.println("a0="+a0);//如果不设置为静态变量会报错,因为a0是类中属性,
                                //使用必须创建类的实例
        System.out.println("a1="+a1);//如果不初始化,会报错。
        Integer a2=new Integer(1);
        System.out.println("a2="+a2);
        Integer a3 = 2;//自动包装;
        int a4 = a2.intValue();//自动拆包
        System.out.println("a3="+a3+",a4="+a4);

        Scanner console  = new Scanner(System.in);
        ArrayList<Integer> al = new ArrayList<>();
        while(/*console.hasNext()&&*/console.hasNextLine()){//console.hasNext()&& 

            if(al.size()==2)
                break;
            int a = console.nextInt();
            al.add(a);  
            System.out.println("in_al="+al);
        }
        System.out.println("al="+al);
        ArrayList<Integer> al1 = new ArrayList<>();
        while(console.hasNext()){
            int b = console.nextInt();
            al1.add(b);
            System.out.println("in_al1="+al1);  

            System.out.println(al1.get(0));
            if(al1.size()==(4*al1.get(0)+1))
                break;  
        }
        System.out.println("al1="+al1);
    }

}
  • 输出结果
    这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值