Java find array element repeat times and Java string comparison size sample _java

Source: Internet
Author: User
Tags first string

Copy Code code as follows:

/**
* Name: Find the number of times the elements in the array are repeated to multiple and repeat the number of times
* Description:
* The elements in the array may be duplicated, and this method can find the number of repetitions and how many times it can be returned.
* But need to know the largest element of this array is how much, if not sure, it's tragic ~
*
* @param array of array targets;
The maximum value of the data in the Max array;
* @return Returns a map set containing the maximum number of repetitions (value) and the number of repetitions (maxcount);
* Internal exception, the default return 0;
* @throws
* @Author Yang Yuan
*/
public static map<string, integer> Arraysearch (int[] Array,int max) {
Result set
map<string, integer> resultmap = new hashmap<string, integer> ();
Number of repetitions
int maxcount = 0;
Number of repeat times to many
int value = 0;

try{
Initializes an array of data that holds the number of occurrences of each element
int[] DataArray = new int[max+1];

Iterate over the array to find, with each element as subscript, directly locate the data array, and do +1 operations, indicating that a
for (int i:array) {
dataarray[i]++;
}

Find the maximum value in the data array
for (int i=0;i<dataarray.length;i++) {
if (Dataarray[i]>maxcount) {
Maxcount=dataarray[i];
Value=i;
}
}
}catch (Exception e) {}

Resultmap.put ("Maxcount", Maxcount);
Resultmap.put ("value", value);

return resultmap;
}

/**
* Name: Compare two string sizes
* Description: The rules of comparison are consistent with the order by effect in the database;
* NULL automatically to null, empty string maximum;
*
* @param first string to compare;
* Second the second string to be compared;
* @return Returns a positive value greater than second;
* The second returns 0;
* Second returns a negative number;
* Internal exception returned by default 0;
* Return value is not fixed value oh ~ ~;
* @throws
* @Author Yang Yuan
*/
public static int comparestring (String first,string second) {
int result = 0;

try{
Null empty
A-First==null? "": A;
Second = Second==null? "": Second;

Pre-record string lengths to avoid repeated reads
int Firstlength=first.length ();
int Secondlength=second.length ();

Handling special cases containing empty strings
if ("". Equals (a) | | "". Equals (second)) {
Who is long and who is small
result = Secondlength-firstlength;
}else{
Temporary space, used to hold the total ASCII code
int firstcount = 0;
int secondcount = 0;
Using pure arithmetic to derive the smaller number of two numbers, it is BT
int minlength = (secondlength* (firstlength/secondlength) + firstlength* (secondlength/firstlength))/(firstLength/ Secondlength + secondlength/firstlength);
To intercept by a bit by a short number of digits in two strings to prevent the crossing
for (int i=0;i<minlength;i++) {
Ask for ASCII code and
Firstcount+=first.substring (i,i+1). GetBytes () [0];
Secondcount+=second.substring (i,i+1). GetBytes () [0];
And not equal, indicating that the size has been compared
if (Firstcount!=secondcount) {
Break
}
}

if (Firstcount==secondcount) {
A long length of large
result = Firstlength-secondlength;
}else{
The sum of large big
result = Firstcount-secondcount;
}
}
}catch (Exception e) {}

return result;
}

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.