Java_Day15 (one-dimensional array, the main parameter of the mystery, the array copies)

One-dimensional array

  • Java array is a reference data type (basic data elements can be stored, itself a reference data type).
  • An array is a collection of data.
  • Arrays can store basic data types, data types may be stored reference (a reference address data, 4 bytes).
  • Similar to the c
  • Array object heap memory.
  • Once you create the array can not be changed.
  • All have an array of objects have lengthAttributes, Used to obtain the number of elements in the array obtained.
  • int int type can store type (4 bytes), char (2 bytes).
  • the main method to obtain a String parameter array. // c ++ form which may be employed: i.e., (String args []) in brackets after the variable name.
  • The first element of the array as a memory address of the array.
  • Advantages: high retrieval efficiency.
  • Disadvantages: In order to ensure the memory address of each array element is continuous, it is deleted and increased random element in the array when, less efficient, because the random element will involve additions and deletions to the operating elements of unity behind forward and backward displacement.
    Why can not store large data arrays of data?
    Because very, very difficult to find a very large block of contiguous space on the memory.
    An array of additions and deletions additions and deletions did not affect the last element.

Array definition

  • How declare / define a one-dimensional array? // c ++ put in parentheses behind.
  • int[] array1;
  • double[] array2;
  • boolean[] array3;
  • Object[] array4;
  • String[] array5;

One-dimensional array initialization

How to initialize a one-dimensional array of it?
Static initialization:
int [] = {Array} 100,2100;
Object [] = {Objects new new Object (), new new Object ()};
Dynamic initialization:
int [] Array = new new int [. 5]; // default value of 0
String [] names = new String [ 6]; // null default value
when static initialization, when dynamic initialization?
When you create a data store of know -> static.
I do not know do not know when you create -> Dynamic.

Parameter passing

How to call time-dependent shape function parameter is an array?
1. The argument passed an array name.
== 2. Direct transfer a static array (easy).
SUM (new new int [] {l, 2,3});
3. a dynamic array directly transmitted
sum (new int [3]) ;

String main methods [] args

  • JVM calls, will pass a String array over.
  • JVM default length of the passed array of objects this is?
    0Equivalent to String [] strs = {};
    When does it value?
    In fact, this array is left to the user, the user can input parameters on the console, this parameter will be converted to a "String [] args"
    such as: the Java Test1 abc def xyz
    the JVM will abc def xyz loaded into an array of characters: { "abc", "def", "xyz"}
    in the idea, run-> edit config ... -> program arguments entered.
    Therefore, the above main method String [] args array is mainly used to receive user input parameters.

    Applications:
    simulation of a system, assuming that the system you want to use the user name and password, no user name and password can not be used.
public class Test2 {
	public static void main(String[] args) {
		if(args.length!=2) 
			System.out.println("使用该系统,请输入用户名和密码,中间以空格分隔");
			return ;
	String username = args[0];
	String password = args[1];
	if("xinkong".equals(username)&&"123".equals(password)) {
		System.out.println("登入成功!");
	}
	else {
		System.out.println("登入失败");
}
}
}

An array of reference data types:
calling method
for (int I = 0; I <animals.length; I ++)
Animals [I] .move ();

  • Key:The method of the upward transition when the call, there is the parent class can be called directly, if the method is unique subclass, the subclass needs to downcast, call again. (Polymorphic)
  • Expansion of one-dimensional array
    to build a large array, and then copy it into the array.
    Method: System.arraycopy ();
    directly, sun company written.
    Copy array
    // copy source
    int [] = {1,11,22,3,4 the src};
    // copy destination array
    int [] dest = new new int [20 is];
    // method, a starting point of the source array , starting from the target 3 array, 2 is a copy length
    System.arraycopy (src, 1, dest, 3,2)
Published 50 original articles · won praise 8 · views 3068

Guess you like

Origin blog.csdn.net/jiahuan_/article/details/105082138