Java Base (array) array to sort and find if there is a number in the array

Source: Internet
Author: User

Array sorting and arrays containing a number

Import Java.util.Arrays;
Class Demo4
{
public static void Main (string[] args)
{

Tools for use in arrays: Arrays

Int[] arr = {1,2,4,5};

Finds if there is a number in the array
int num = 3;

There: corresponding index location
int result = Arrays.binarysearch (arr,num);

SYSTEM.OUT.PRINTLN (result); -3

Do sort:

Int[] arr1 = {6,2,7,5,8};
Arrays.sort (ARR1);

for (int i = 0;i < arr1.length; i++)
{
System.out.println (Arr1[i]);
}
}
}


Enter a number to get each individual bit of it
Class Demo2
{
public static void Main (string[] args)
{

/* Enter an integer: 678---> 8,7,6*/
----goal is to train everyone how to get a number of individual bits.
int num = 678;

Take out every bit of this number.
Bit
int g = num% 10;
int s = (NUM/10)% 10;
int p = (num/100);


}
}

Create an int type
Class Demo3
{
public static void Main (string[] args)
{

Create an array of type int
Arr: is a variable just a reference address to an array
Local variables exist in the stack
Member variables--Properties---> in the heap area
Static variable---> Shared area
int[] arr = new int[2];
ARR[1] = 10;

arr = null;


int[] arr1 = new int[2];
ARR1[1] = 20;

System.out.println (arr[1]);//10

int[] arr2 = arr;
ARR2[1] = 30;

System.out.println (arr[1]);//10

}
}

Insert songs sorted by initials to a specified location
Class Demo1
{
public static void Main (string[] args)
{

/*
5. Array of songs as follows

string[] musics = new string[]{"Island", "Ocean", "Pretty", "Sun"};
This is an alphabetical sequence of string arrays

Now I'm going to insert a new English song Inside,
Insert to the specified position by first alphabetical order

Tip: Musics[i].comparetoignorecase (music) > 0
The above method is a method of comparing strings,
If the previous value returns 1 large, if the back is greater than the preceding, returns-1, or equals the preceding, returns 0

*/
string[] musics = new string[]{"Island", "Ocean", "Pretty", "Sun"};

1. Sort the songs by bubbling
for (int i = 0;i<musics.length-1;i++) {

for (int j = 0; j<musics.length-1-i;j++) {

if (Musics[j].comparetoignorecase (musics[j+1]) >0) {

If the front is larger than the back, swap position
String temp = musics[j];
MUSICS[J] = musics[j+1];
MUSICS[J+1] = temp;


}
}
}

Insert a Song
string[] Newmusics = new string[musics.length+1];

To define an inserted song
String songname = "Love";


Use loops to compare Songname to the original song list

The default is the first one
for (int i = 0;i<musics.length +1;i++) {

if (i = = 0) {

Newmusics[i] = Songname;
}


Newmusics[i] = musics[i-1];

}

Sorts the array in one insert. /* Check the information * *


Iterating through an array
for (int i = 0;i<musics.length;i++) {

System.out.println (Musics[i]);

}
}
}

Java Base (array) array to sort and find if there is a number in the array

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.