Java's array types (defined arrays, use the array, an array of in-depth, converted into yuan float reading of string)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_38358499/article/details/98640247

Definition array

  • Array is a reference type variable, the use of an array to define a reference variable corresponding to a pointer defined, has not to any valid memory defining, and therefore can not specify the length of the array defining an array, the array is initialized when the can after use
  • Array initialization:
  1. Static initialization
    public class ArrayTest {
    	public static void main(String[] args)
    	{
    		//定义一个int数组类型的变量,变量名为intArr,并静态初始化
    		int[] intArr;
    		intArr = new int[] {5,6,8,20};
    		
    		//定义一个Object数组类型的变量,变量名为objArr,并静态初始化
    		Object[] ObjArr;
    		ObjArr = new String[] {"Java","李刚"};
    		Object[] ObjArr1;
    		ObjArr1 = new Object[] {"Java","李刚"};
    	}
    }

    Description: define and initialize arrays simultaneously completed, abbreviated: int [] a = {5,6,7,9};

  2. Dynamic initialization

        语法:int[] prices = new int[5]; Object[] books = new String[4];

Using an array (the foreach loop)

Access array elements, the array elements and assign array element value removed. Note: When the specified index to access array elements is less than 0 or greater than or equal to the length of the array, the compiler without error, but there will be a runtime exception.

Automatic foreach loop through the array and each element of the collection, use the following:

public class ForEachTest 
{
	public static void main(String[] args)
	{
		String[] books = {"疯狂Java讲义","李刚","使用forEach循环遍历"};
		for (String book : books)
		{
			System.out.println(book);
		}
	}
}

Note: do not need a length of the array and a set of array elements and without the need to access a set of elements based on the index, the foreach loop variable corresponds to a temporary variable, not the array elements, if you want to change the values ​​of the array elements can not be used foreach loop

 In-depth array

  • An array of memory: the actual array is stored in the heap, if the array reference to the array object reference variables, were stored on the stack. Heap memory object does not end with the destruction process and, with the method of execution stack ends are naturally destroyed
    public class ArrayInRam
    {
    	public static void main(String[] args)
    	{
    		int[] a = {5, 3, 4, 8};
    		int[] b = new int[4];
    		System.out.println(a.length);
    		System.out.println(b.length);
    		for(int i = 0; i < a.length; i++) 
    		{
    			System.out.println(a[i]);
    		}
    		for(int i = 0; i < b.length; i++) 
    		{
    			System.out.println(b[i]);
    		}
    		b = a;
    		System.out.println(b.length);
    	}
    }
    Note: When the element b is undefined, the output o. Back for large loop with not with ";", the program performed as usual, not error
  • Initialize an array of reference types: first given a Person class, get a different person's age and height
    class Person
    {
    	public int age;
    	public int heigth;
    	public void info()
    	{
    		System.out.println("我的年龄是:" + age + ", 我的身高是:" + heigth);
    	}
    }
    
    public class ReferenceArrayTest
    {
    	public static void main(String[] args)
    	{
    		Person[] students = new Person[2];
    		
    		Person zhang = new Person();
    		zhang.age = 15;
    		zhang.heigth = 168;
    		
    		Person lee = new Person();
    		lee.age = 17;
    		lee.heigth = 170;
    		
    		students[0] = zhang;
    		students[1] = lee;
    		
    		students[0].info();
    		students[1].info();
    	}
    }

    Note: When defining an object, followed by (), is followed by using a method (), student [0] to access and zhang Person instance variables and instance methods exactly the same effect

  • No multi-dimensional array in Java, Java language array type is a reference type. When the array element reference point is really an array of memory again, it looks like a multidimensional array. When multidimensional array initialization, you can define the size of the leftmost dimension, you can also specify the size of each dimension of time

 Function: Convert floating RMB phonetic string into

import java.util.Arrays;

public class Num2Rmb 
{
	private String[] hanArr = {"零","壹","二","叄","肆","伍","陆","柒","捌","玖"};
	private String[] unitArr = {"十","百","千"};
	//将整数和小数部分分开
	private String[] divide(double num)
	{
		long zhengshu = (long)num;
		long xiaoshu = Math.round((num - zhengshu) * 100);
		return new String[] {String.valueOf(zhengshu) + "", String.valueOf(xiaoshu)};
	}
	//把一个四位的数字字符串变换成汉字
	private String toHanStr(String numStr)
	{
		String result = "";
		int numLen = numStr.length();
		for(int i = 0; i < numLen; i++)
		{
			//char型数字转换成int型数字
			int num = numStr.charAt(i) - 48;
			if(i != numLen -1 && num !=0)
			{
				result += hanArr[num] + unitArr[numLen - 2 - i];
			}
			else
			{
				result += hanArr[num];
			}
		}
		return result;
	}
	
	public static void main(String[] args)
	{
		Num2Rmb n = new Num2Rmb();
		System.out.println(Arrays.toString(n.divide(236711125.123)));
		System.out.println(n.toHanStr("6109"));
	}
}

Description: Math.round () a number rounded to the nearest integer; .charAt () Returns the string specified position, a single string; String.valueOf () Returns the value of type string; an array of Arrays.toString conversion a string

 

 

Guess you like

Origin blog.csdn.net/qq_38358499/article/details/98640247