JAVA course 4 (array), java learning Array
Array:
A collection of the same type of data, that is, a container.
Definition method:
Int [] atrr = new int [5]; // array name of the int type, arr, new dynamic open int [5], the array size is 5, can only store 5 elements,
Subscript 0-4
The basic distribution in the memory after the array is defined:
Memory Division:
1. Register-> CPU
2. Local Method Area, related to the system
3. Method Area
4. stack memory:
Local variables are stored, and the current variables are automatically released once the scope of the variables ends.
Stack memory is updated quickly because the life cycle of the variables in the stack is very short (release after the end ).
For example, a local code block defines the lifecycle of a local variable. In some large programs, the main function does not end.
The variables are not released, so local code blocks are referenced.
Sample Code:
Import org. omg. cosNaming. namingContextExtPackage. addressHelper; public class Main {public static void main (String [] args) {int [] arr = new int [6]; System. out. println (arr [1]); {// partial code block, which is valid in {}. {} execute play age to automatically release int age = 3; System. out. println (age);} System. out. println ();}}
5. heap memory :,
Objects (arrays) are stored in the heap. All new objects are stored in the heap, And the heap will not be released at any time. The heap stores objects, loads data in objects, and damages data, other data can still be used. However, if one stack data is corrupted, none of the others will be used.
Features:
1. Each object in the heap has a first address value.
2. each local variable in heap memory has a default initialization value. The initialization value varies depending on the type. For example, if the int type is 0, float is 0.0f \ 0.0, and boolean is false, char special '\ u000000', \ u escape, unicode
3. Release Method: Garbage Collection Mechanism
X = y;
Null:
If you do not want to use an arr object, arr is an array. If you want arr to do not point to any object, arr = null, but the heap does not release the arr address,
Treat it as garbage. The garbage in the heap is automatically recycled from time to time, So java is superior to C ++ in terms of memory management.
Import org. omg. cosNaming. namingContextExtPackage. addressHelper; public class Main {public static void main (String [] args) {int [] arr = new int [] {1, 1, 1, 1}; System. out. println (arr [1]); arr = null; // when the referenced variable does not point to any entity, it also references this entity, and an exception is thrown. out. println (arr [2]);}
Use small exercises for Arrays:
Import org. omg. cosNaming. namingContextExtPackage. addressHelper; public class Main {public static void main (String [] args) {int [] arr = new int [5]; int [] B = new int [5]; arr [0] = 5; B [0] = 6; B [3] = 15; System. out. println ("arr [0] =" + arr [0]); System. out. println ("arr [3] =" + arr [3]); arr = B; // change the direction of arr to System. out. println ("arr [0] =" + arr [0]); System. out. println ("arr [3] =" + arr [3]); System. out. println ("B [3] =" + B [3]); System. out. println ("B [0] =" + B [0]); B = null; // B does not point to System. out. println ("arr [0] =" + arr [0]); System. out. println ("arr [3] =" + arr [3]); // System. out. println ("B [3] =" + B [3]); treated as garbage // System. out. println ("B [0] =" + B [0]);}
Small issues with Arrays
Import org. omg. cosNaming. namingContextExtPackage. addressHelper; public class Main {public static void main (String [] args) {int [] arr = new int [5]; int [] B = new int [5]; system. out. println (arr); // [I @ 15db9742, 15db9742 is the hash value stored by arr, [-> array, I-> int type // so we don't know, when the current object is of any type, you can directly name the current object }}
Another way to define arrays:
Format 2: known array elements, only need a container to store
Public class Main {public static void main (String [] args) {int [] arr = new int [5]; // int [] B = new int [] {1, 2, 3,4, 5}; same as int B [] = }; // same as int len = B. length; for (int I = 0; I <len; I ++) {System. out. println ("B [" + I + "] =" + B [I]) ;}}
Array exercises:
Hexadecimal conversion
Import java. awt. print. printable; import java. util. arrays; import java. util. export; import javax. swing. text. defaultEditorKit. insertBreakAction; import org. omg. cosNaming. namingContextExtPackage. addressHelper;/* requirement: * obtain the X-base representation of an integer. **/public class Main {public static void main (String [] args) {cin = new partition (System. in); int num = cin. nextInt (); int ary [] = new int [8]; getx (num, ary, 15, 4); // hexadecimal Sy Stem. out. println ("hexadecimal representation:"); print (ary); int bry [] = new int [12]; int num1 = cin. nextInt (); getx (num1, bry, 7,3); // an 8-digit System. out. println ("octal representation:"); print8 (bry); System. out. println ("the binary representation of 6 is as follows:"); System. out. println (Integer. toBinaryString (6); System. out. println ("-6 binary representation:"); System. out. println (Integer. toBinaryString (-6); cin. close () ;}public static void print (int [] ary) {int I; for (I = 7; I> = 0; I --){ If (ary [I]! = 0) break;} for (int j = I; j> = 0; j --) {if (ary [j]> 9) switch (ary [j]) {case 10: System. out. print ("A"); break; case 11: System. out. print ("B"); break; case 12: System. out. print ("C"); break; case 13: System. out. print ("D"); break; case 14: System. out. print ("E"); break; case 15: System. out. print ("F"); break; default: break;} else {System. out. print (ary [j]); ;}} System. out. println ();} public static void print8 (int bry []) {int I; for (I = 10; I> = 0; I --) {if (bry [I]! = 0) break;} for (int j = I; j> = 0; j --) {System. out. print (bry [j]);} System. out. println ();} public static void getx (int num, int ary [], int mod, int wz) {int l = 0; for (int I = 0; I <8; I ++) {int t = num & mod; // maximum value of hexadecimal ary [l ++] = t; num = num >>> wz; // four binary bits represent a hexadecimal number }}}
How to Learn the array chapter in JAVA Learning
Now you have to be familiar with it. If you want to use it freely, you can only do some small projects or
I can understand it only when I write a small program.
You can simply think of it as a box that can hold many values, and this box contains things.
It is ordered.
Therefore, you need to retrieve the items (that is, the values) from the box according to the specified "Index.
Even if you don't understand it now, it's okay if you know it. I did not know when I learned it, but I forgot it. But after a while, I realized it.
Java contains arrays a {,} and arrays B {, 5}. Now we need to update and synchronize arrays B.
According to your requirements, the elements in B must be the same as those in a, but you cannot directly assign the elements in a to B.
The following code can be implemented. You can see it by yourself, with comments.
Import java. util. arrayList; import java. util. iterator; import java. util. list; public class Test {/*** @ param args */private List <Integer> asList (int [] I) {List <Integer> list = new ArrayList <Integer> (); for (int t: I) {list. add (t) ;}return list;} public static void main (String [] args) {// TODO Auto-generated method stub Test t = new Test (); int [] a = {1, 2, 3, 4}; int [] B = {3, 4, 5}; List < Integer> listA = t. asList (a); List <Integer> listB = t. asList (B); for (int I: listA) {// traverses a. for each element in a, if (! ListB. contains (I) {// If B does not exist, add it to listB in B. add (I) ;}for (Iterator <Integer> it = listB. iterator (); it. hasNext ();) {// traverse each element in B int I = it. next (); if (! ListA. contains (I) {// If a does not exist, delete it from B. remove () ;}} System. out. println ("a:" + listA); System. out. println ("B:" + listB );}}