Java object-Oriented---object container

Source: Internet
Author: User

Generic-Class--arraylist<>;

2. Each element in an object array is the object's manager and not the object itself!!!!!

Basic data types for 3.java classes

 

Basic data types Packing class

Byte

Byte
Short Short
Int Integer
Long Long
Float Float
Double Double
Char Character
Boolean Boolean

The application of 4.for-each loop in object array

In an array of basic types, such as int[], you can iterate through the values in the array using the For-each loop, but you cannot modify the values in the array because using For-each to get a copy of the arrays instead of the actual variables in the array. Therefore, the modification of the elements in the original array cannot be realized;

In an object class array, an element in an object class array is not a real variable, but rather a manager of an object than the object itself. So, when you do a For-each loop on an object class array,

For example: for (String s:a) {

System.out.println (s);

s = "Hello World";

}

This is equivalent to each element of the object class array A (note: At this point the element in a is the object's manager is not the object itself) assigned to S, that is, the variable s and the elements in the array a[i] together to manage the real variable;

It is also equivalent to modifying the element pointed to by the variable s to modify the variable that the object points to in the object class array.

5.

private int i;

Public String toString () {

Return "" +i;

}

The toString function is very magical, defining the function in the class (note: The function must be named and manipulated exactly the same), and the object that directly prints the class can be printed, such as SYSTEM.OUT.PRINTLN (the object name of the class);

6.SET---Collection

hashset<string> s = new hashset<string> ();
S.add ("first");
S.add ("second");
S.add ("first");
S.add ("first");
S.add ("first");
for (String k:s) {
System.out.println (k);
}

Set is similar to ArrayList, which is one of the containers, and set is the same in Java as the set in mathematics, with disorder and element uniqueness.

7. hash Table---HashMap

Package coins;

Import Java.util.HashMap;
Import Java.util.Scanner;

public class Coin {
Private Hashmap<integer, string> coinnames = new hashmap<integer,string> ();
Public Coin () {
Coinnames.put (1, "penny");
Coinnames.put ("Dime");
Coinnames.put ("quarter");
Coinnames.put ("Half-Dollar");
Coinnames.put (50, "five hairs");

System.out.println (Coinnames);
System.out.println (Coinnames.keyset ());
System.out.println (Coinnames.keyset (). Size ());

Traverse HashMap
for (int i:coinnames.keyset ()) {
String s = coinnames.get (i);
System.out.println (s);
}

}
Public String getName (int amount) {
if (Coinnames.containskey (amount))
return Coinnames.get (amount);
Else
Return "Not FOUND";
}
public static void Main (string[] args) {
Scanner in = new Scanner (system.in);
int amount = In.nextint ();
Coin Coin = new Coin ();
String name = Coin.getname (amount);
SYSTEM.OUT.PRINTLN (name);
System.out.println (Coin.getname (amount));

}

}

A hash table is also a container in which data is stored as a key-value pair in the HashMap, which is a key corresponding to a value. Each key is unique and cannot be duplicated.

Java object-Oriented---object container

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.