Java中的方法整理

这篇博客详细整理了Java编程中的方法,包括计算数组长度、生成随机数、数组和ArrayList的操作,以及方法定义、对象创建、变量比较、类型转换等方面的知识。还介绍了变量命名规则、用户交互和继承的概念。
摘要由CSDN通过智能技术生成

1、计算一组有多少个术语个数
int 名称=要计算的组名.length

public class SimpleArray {
   
	public static void main(String[]args){
   
		String[] wordListone={
   "24/7","nmultiTier","30000 foot","B-to-B","win-win","front-end","web-based"};
		String[] wordListtwo={
   "empowered","sticky","value-added","oriented","centric"};
	    int[] wordListthree={
   1,3,4,5,6};
		
		int oneLength=wordListone.length;//计算一组有多少个
		int twoLength=wordListtwo.length;
		int threeLength=wordListthree.length;
		System.out.println(oneLength);
		System.out.println(twoLength);
		System.out.println(threeLength);
	}

}

2、产生随机数字
产生随机数字的结构:数值类型 变量名称=(数值类型)(Math.random())
PS:直接产生的数值会介于0~1之间的小数所以可以进行转换


public class Random {
   
	public static void main(String[]args){
   
		int c=(int)Math.random();//转换,将double类型转换为int类型
		double x=Math.random();//产生一个随机数,仅仅会介于0和1之间
		int y=5;
		int z=(int)(x*y);//随机数与定义的数字相乘,转换
		System.out.println(c);
		System.out.print(z);
		}

}

3、数组
一、一般数组
一般数组中的各种操作


public class PracticeNormalClass1 {
   
	public static void main(String[]args){
   
		String [] myList=new String[2];//定义普通数组
		System.out.println(myList);
		String s=new String("nihao");
		myList[0]=s;                   //将s赋予普通数组第一个位置
		String s1=new String("a");
		myList[1]=s1;
		int size=myList.length;        //计算这个普通数组的大小
		System.out.println(size);
		String s2=myList[1];           //获取myList中1位置的值
		System.out.println(s2);
		myList[1]=null;                //移除myList中1位置的值
		boolean b=false;               //判断某个特定元素是否在数组中
		for(String item:myList){
   
			if
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值