Java array and sample code _java

Source: Internet
Author: User
Tags arrays

The following is a 12 array operation method that obtains the maximum number of votes in the related question about array methods in StackOverflow.

1. Declare an array

string[] Aarray = new String[5]; 
String[] Barray = {"A", "B", "C", "D", "E"}; 

2. Output an array

Int[] Intarray = {1, 2, 3, 4, 5}; 
String intarraystring = arrays.tostring (intarray); 
  
Print directly'll print reference value 
System.out.println (intarray); 
[I@7150bd4d 
  
System.out.println (intarraystring); 
[1, 2, 3, 4, 5] 

3. Create an array list from an array

String[] Stringarray = {"A", "B", "C", "D", "E"}; 
arraylist<string> ArrayList = new Arraylist<string> (arrays.aslist (Stringarray)); 
System.out.println (arrayList); 
[A, B, C, D, E] 

4. Check whether an array contains a value

String[] Stringarray = {"A", "B", "C", "D", "E"}; 
Boolean B = Arrays.aslist (Stringarray). Contains ("a"); 
System.out.println (b); 
True 

5. Connect two arrays

Int[] Intarray = {1, 2, 3, 4, 5}; 
Int[] IntArray2 = {6, 7, 8, 9, ten}; 
Apache Commons Lang Library 

6. Declare an inline array (array inline)

Method (New string[]{"A", "B", "C", "D", "E"});

7. Put the provided array elements into a string

Containing the provided list of elements 
//Apache Common lang 
String j = stringutils.join (new string[] {"A" , "B", "C"}, ","); 
System.out.println (j); 
A, B, c 

8. Convert an array list to arrays

String[] Stringarray = {"A", "B", "C", "D", "E"}; 
arraylist<string> ArrayList = new Arraylist<string> (arrays.aslist (Stringarray)); 
string[] Stringarr = new string[arraylist.size ()]; 
Arraylist.toarray (Stringarr); 
for (String S:stringarr) 
  System.out.println (s); 

9. Convert an array to a set (set)

set<string> set = new Hashset<string> (arrays.aslist (Stringarray)); 
SYSTEM.OUT.PRINTLN (set); 
[D, E, B, C, a] 

10. Reverse an array

Int[] Intarray = {1, 2, 3, 4, 5}; 
Arrayutils.reverse (intarray); 
System.out.println (arrays.tostring (Intarray)); 
[5, 4, 3, 2, 1] 

11. Remove elements from the divisor group

Int[] Intarray = {1, 2, 3, 4, 5}; 
int[] removed = Arrayutils.removeelement (Intarray, 3);//create a new array 
System.out.println (arrays.tostring ( removed)); 

12. Convert an integer to a byte array

byte[] bytes = bytebuffer.allocate (4). Putint (8). Array (); 
  
for (byte t:bytes) { 
  System.out.format ("0x%x", t); 
} 

 

The above is the Java array Data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Related Article

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.