Well off with you to learn JAVA -------- array introduction, java -------- Array

Source: Internet
Author: User

Well off with you to learn JAVA -------- array introduction, java -------- Array

Today, I will share with you an important data type in JAVA learning-array.

To store a series of related data, using arrays is a good choice. In addition, if a program fragment often appears repeatedly, defining it as a method can effectively simplify the program code. An array is a data type composed of a group of variables of the same type. They are represented by a common name, and individual elements in the array are marked to indicate their locations.

To use an array of Java, two steps must be taken: (1) declaring the array and (2) Allocating the memory to the array.

The syntax for these two steps is as follows:

The above is a brief introduction to arrays. in JAVA programming, arrays are often used, especially the memory allocation space of arrays, which may not be very understandable at the beginning, I will continue to share with you some examples of arrays in the future.

 


How to Write shuffle programs using java arrays? I just learned java. I 'd like to give you more advice,

Package xly04;
Import java. util. Random;
Public class ArrayDemo3 {
/**
* Array application:
* 1 random shuffling
* 2 sequential Licensing
*/
Public static void main (String [] args ){
String [] cards = new String [] {"Black peach a", "Black peach 2", "Black peach 3", "Black peach 4 ",
"Black peach 5", "Black peach 6", "Black peach 7", "Black peach 8", "Black peach 9", "Black peach 10", "Black peach J", "Black peach Q ", "Black peach K ",
"Hongtao a", "Hongtao 2", "Hongtao 3", "Hongtao 4", "Hongtao 5", "Hongtao 6", "Hongtao 7", "Hongtao 8 ", "Hongtao 9 ",
"Peach 10", "Peach J", "Peach Q", "Peach K", "square A", "square 2", "square 3", "Square 4 ", "block 5 ",
"Square 6", "square 7", "square 8", "square 9", "square 10", "square J", "square q", "square K ", "plum blossom ",
"Plum Blossom 2", "Plum Blossom 3", "Plum Blossom 4", "Plum Blossom 5", "Plum Blossom 6", "Plum Blossom 7", "Plum Blossom 8", "Plum Blossom 9 ", "Plum Blossom 10 ",
"Plum Blossom J", "Plum Blossom Q", "Plum Blossom K", "King", "King "};
// Random random = new Random (); ...... (1)
// Int index = random. nextInt (10); // [0, 10) % 10
// System. out. println (index );
// String card = cards [random. nextInt (cards. length)]; // The length is 8 and the random subscript is [0, 8)
// System. out. println (card); // random card
For (int I = cards. length-1; I> = 1; I --){
Int j = new Random (). nextInt (I); // or (1) do not deregister int j = random. nextInt (I );
... The remaining full text>

Array problems in JAVA

The second problem is that Java cannot directly provide syntax support, but we can continue to implement it.

When you save data, you still use a common array and use letters as subscripts to convert letters into numbers. There are many conversion methods, such:

Private static int [] data = {123,456,789 };
Public static int get (char pos)
{
Return data [Character. toLowerCase (pos)-'a'];
}
Use System. out. println (get ('B'); to check the output.

Java. util. Map and other related classes are required for more complex replacements. For example: "A"-> first, "B"-> second, "C"-> third.
Private static int [] data = {123,456,789 };
Private static Map <String, Integer> posMap = new HashMap <String, Integer> ();
Static
{
PosMap. put ("A", 0 );
PosMap. put ("B", 1 );
PosMap. put ("C", 2 );
}
Public static int get (String pos)
{
Return data [posMap. get (pos)];
}
Use System. out. println (get ("C"); to check the output.

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.