Java programming 46-array Example 2

Source: Internet
Author: User
Tags array example array length

Java programming those things 46-array use Example 2 Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 6.3.4 judge whether array elements are repeatedRequirement: Determine whether the same element exists in an array. If the same element exists, "DUPLICATE" is output; otherwise, "No duplicate" is output ". If you need to judge whether the elements in the array are repeated, You need to compare the elements in the array by two. If any group of elements are equal, the elements in the array are repeated, if you do not want to wait for any group of elements, the elements in the array are not repeated. Implementation principle: assuming that the elements in the array are not repeated and the elements in the array are compared in pairs, the first element in the array is compared with all subsequent elements, and then the second element in the array is compared with the subsequent elements, and so on. If a group of elements are the same, the array is retained and the loop ends. Store the comparison results in a variable, and finally judge the value of the variable. The implementation code is as follows: int [] n = {1, 3, 1, 0}; Boolean flag = true; // assume that the for (INT I = 0; I <n. length-1; I ++) {// The starting element of the loop for (Int J = I + 1; j <n. length; j ++) {// all subsequent elements of the loop // if they are equal, duplicate if (N [I] = N [J]) {flag = false; // set the flag variable to repeated break; // end loop }}// determine the flag variable if (FLAG) {system. out. println ("not repeated");} else {system. out. println ("repeated");} in this Code, whether the flag variable storage is repeated, true indicates not repeated, and false indicates repeated. In an External Loop, variable I represents the subscript of the first element. In an internal loop, variable j Represents the subscript of subsequent elements. When I is zero, it is compared with all subsequent elements, when I is 1, it is also compared with all subsequent elements, and so on, so that the comparison between all elements is realized. If the elements are the same, there are duplicates. Set the flag variable value to flase to end the loop. Finally, you can judge whether the flag is repeated based on the value of the Flag variable. 6.3.5 determine whether the array is symmetricRequirement: Determine whether the array elements are symmetric. For example, {1}, {, 1}, {,} are symmetric arrays. This question is used to determine the element in the array about the center symmetry, that is, the first element in the array is the same as the last element, the second element in the array is the same as the last element, and so on, if the comparison is in the middle, all elements are the same, the array is symmetric. Implementation idea: take the half of the length of the array as the number of cycles, assuming the variable I from 0 to the center of the array, the subscript of the corresponding element is the length of the array-i-1, if a group of elements is not equal, the array is asymmetrical. If all corresponding elements are the same, the array is symmetric. The implementation code is as follows: int [] n = {, 1}; Boolean flag = true; // suppose symmetric for (INT I = 0; I <n. length/2; I ++) {// loop array length half times // compare element if (N [I]! = N [n. length-I-1]) {flag = false; // asymmetric break; // end loop} If (FLAG) {system. out. println ("symmetric");} else {system. out. println ("asymmetric");} in this Code, the flag is used as the flag variable. If the value is true, it indicates symmetric. If the value is false, it indicates asymmetric, you only need to compare the length of the array half a time. If the corresponding elements are different, the array is asymmetrical and the loop ends. Finally, determine the value of the Flag variable to obtain whether the array is symmetric. 6.3.6 digital conversionRequirement: convert a decimal number to a binary number. As mentioned earlier, the Division Two remainder method is generally used when converting a decimal number to a binary number. This method is very regular and can be implemented through loops in the program, in the program, you only need to store the obtained numbers. Implementation idea: store the first number obtained except the second remainder in the first element of the array, the second remainder in the array, and so on, finally, output the obtained number in reverse order. The implementation code is as follows: int n = 35; int [] M = new int [32]; // split the number int num = 0; while (n! = 0) {M [num] = n % 2; // store the remainder num ++; // increase the number by 1 N/= 2; // remove the remainder} // output the split number for (INT I = num-1; I> = 0; I --) {system. out. print (M [I]);} system. out. println (); in this Code, because Int Is 32-bit, you only need an array with a maximum length of 32. The first number to be split, that is, the low position of the binary, is stored in the first element of the array. Num indicates the number of the split numbers and the array subscript, the value of splitting until n is zero and ends. After the loop ends, because the number of split numbers is num, you only need to reverse the output array 0 to the num-1 of the underlying element.

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.