Char array compare java

Compare Two Java char Arrays Example

You may also like

Compare Two Java byte Arrays Example

Compare Two Java long Arrays Example

Compare Two Java short Arrays Example

Compare Two Java int Arrays Example

Compare Two Java double Arrays Example

Compare Two Java boolean Arrays Example

Compare Two Java float Arrays Example

Create List from Java Object Array Example

Add Comment

Cancel reply

Do this Using Array of Staring, Get Username and ph no from the user in run time and store it. User then should be able to find out the user name for given ph and vice versa..
Array of string*

public class GlobalMembersStringtoint
public static int Main()
int k = 0,m,n,eleCount,i,j,count = 0,ch;
int[] newarray = new int[90];
String String = new String(new char[100]);
String pattern = new String(new char[10]);
System.out.print(“enter the string \n”);
fgets(String, 100, stdin);
System.out.print(“enter the pattern \n”);
fgets(pattern, 100, stdin);
m = pattern.length() – 1;
System.out.printf(“\n the length of pattern = %d”,m);
n = String.length() – 1;
System.out.printf(“\n the length of string = %d”,n);
eleCount = n – m;
System.out.printf(“\n number of substrings = %d”,eleCount);
for (i = 0;i num = 0;
k = i;
for (j = 0;j < m;j++)
temp = (int)String.charAt(k);
num = num * 10 + temp;
k++;
>
newarray[i] = num;
System.out.printf(“the value of new array = %d \n”,newarray[i]);
>
num = 0;
for (j = 0;j < m;j++)
temp = (int)String.charAt(k);
num = num * 10 + temp;
>
System.out.printf(“number = %d \n”, num);
return 0;
>
> friends
help me to convert this code into java i am getting problem ……

Читайте также:  Php href link image

Источник

Java Arrays — equals(char[], char[]) Method

The Java Arrays equals(char[] a, char[] a2) method returns true if the two specified arrays of chars are equal to one another. Two arrays are equal if they contain the same elements in the same order. Two array references are considered equal if both are null.

Declaration

Following is the declaration for java.util.Arrays.equals() method

public static boolean equals(char[] a, char[] a2)

Parameters

  • a − This is the array to be tested for equality.
  • a2 − This is the other array to be tested for equality.

Return Value

This method returns true if the two arrays are equal, else false

Exception

Java Arrays equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) Method

Description

The Java Arrays equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) method returns true if the two arrays of chars in the given ranges are equal to each other. In case of any array null, a NullPointerException is thrown.

Declaration

Following is the declaration for java.util.Arrays.compare(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) method

public static boolean equals​(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)

Parameters

  • a − This is the first array to be tested for equality.
  • aFromIndex − This is the index of the first element (inclusive) of first array to be tested for equality.
  • aToIndex − This is the index of the last element (exclusive) of first array to be tested for equality.
  • b − This is the second array to be tested for equality.
  • bFromIndex − This is the index of the first element (inclusive) of second array to be tested for equality.
  • bToIndex − This is the index of the last element (exclusive) of second array to be tested for equality.
Читайте также:  Параллакс

Return Value

This method returns true if the two arrays, over the specified ranges, are equal.

Exception

  • IllegalArgumentException − if aFromIndex > aToIndex or if bFromIndex > bToIndex
  • ArrayIndexOutOfBoundsException − if aFromIndex < 0 or aToIndex >a.length or if bFromIndex < 0 or bToIndex >b.length
  • NullPointerException − if either array is null

Example 1

The following example shows the usage of Java Arrays equals(char[], char[]) method. First, we’ve created three arrays of chars, and compared them using equals(char[], char[]) method. As per the result, the comparison is printed.

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing three char arrays char[] arr1 = new char[] < 'A', 'D' , 'F' >; char[] arr2 = new char[] < 'B', 'E' , 'F' >; char[] arr3 = new char[] < 'A', 'D' , 'F' >; // comparing arr1 and arr2 boolean retval = Arrays.equals(arr1, arr2); System.out.println("arr1 and arr2 equal: " + retval); // comparing arr1 and arr3 boolean retval2 = Arrays.equals(arr1, arr3); System.out.println("arr1 and arr3 equal: " + retval2); > >

Output

Let us compile and run the above program, this will produce the following result −

arr1 and arr2 equal: false arr1 and arr3 equal: true

Example 2

The following example shows the usage of Java Arrays equals(char[], int, int, char[], int, int) method. First, we’ve created three arrays of chars, and compared them using equals(char[], int, int, char[], int, int) method. As per the result, the comparison is printed.

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing three char arrays char[] arr1 = new char[] < 'A', 'D' , 'F' >; char[] arr2 = new char[] < 'B', 'E' , 'F' >; char[] arr3 = new char[] < 'A', 'D' , 'F' >; // comparing arr1 and arr2 boolean retval = Arrays.equals(arr1, 0, 2, arr2, 0, 2); System.out.println("arr1 and arr2 equal: " + retval); // comparing arr1 and arr3 boolean retval2 = Arrays.equals(arr1, 0, 2, arr3, 0, 2); System.out.println("arr1 and arr3 equal: " + retval2); > >

Output

Let us compile and run the above program, this will produce the following result −

arr1 and arr2 equal: false arr1 and arr3 equal: true

Example 3

The following example shows the usage of Java Arrays equals(char[], int, int, char[], int, int) method. First, we’ve created three arrays of chars, and compared their sub-arrays using equals(char[], int, int, char[], int, int) method. As per the result, the comparison is printed.

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing three char arrays char[] arr1 = new char[] < 'A', 'D' , 'F' >; char[] arr2 = new char[] < 'B', 'E' , 'F' >; char[] arr3 = new char[] < 'A', 'D' , 'F' >; // comparing arr1 and arr2 boolean retval = Arrays.equals(arr1, 2, 3, arr2, 2, 3); System.out.println("Last element of arr1 and arr2 equal: " + retval); // comparing arr1 and arr3 boolean retval2 = Arrays.equals(arr1, 2, 3, arr3, 2, 3); System.out.println("Last element of arr1 and arr3 equal: " + retval2); > >

Output

Let us compile and run the above program, this will produce the following result −

Last element of arr1 and arr2 equal: true Last element of arr1 and arr3 equal: true

Источник

Java Arrays — compare() Method

The Java Arrays compare(char[] a, char[] b) method compares the two arrays of chars lexicographically. In case of null arrays comparison, arrays are considered equals and a null array is lexicographically less than a not null array.

Declaration

Following is the declaration for java.util.Arrays.compare(char[] a, char[] b) method

public static int compare(char[] a, char[] b)

Parameters

  • a − This is the first array to be compared.
  • b − This is the second array to be compared.

Return Value

This method returns the value 0 if the first and second array are equal and contain the same elements in the same order; a value less than 0 if the first array is lexicographically less than the second array; and a value greater than 0 if the first array is lexicographically greater than the second array.

Exception

Java Arrays compare(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) Method

Description

The Java Arrays compare(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) method compares the two arrays of chars in the given ranges lexicographically. In case of any array null, a NullPointerException is thrown.

Declaration

Following is the declaration for java.util.Arrays.compare(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) method

public static int compare​(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)

Parameters

  • a − This is the first array to be compared.
  • aFromIndex − This is the index of the first element (inclusive) of first array to be compared.
  • aToIndex − This is the index of the last element (exclusive) of first array to be compared.
  • b − This is the second array to be compared.
  • bFromIndex − This is the index of the first element (inclusive) of second array to be compared.
  • bToIndex − This is the index of the last element (exclusive) of second array to be compared.

Return Value

This method returns the value 0 if, over the specified ranges, the first and second array are equal and contain the same elements in the same order; a value less than 0 if, over the specified ranges, the first array is lexicographically less than the second array; and a value greater than 0 if, over the specified ranges, the first array is lexicographically greater than the second array.

Exception

  • IllegalArgumentException − if aFromIndex > aToIndex or if bFromIndex > bToIndex
  • ArrayIndexOutOfBoundsException − if aFromIndex < 0 or aToIndex >a.length or if bFromIndex < 0 or bToIndex >b.length
  • NullPointerException − if either array is null

Example 1

The following example shows the usage of Java Arrays compare(char[], char[]) method. First, we’ve created two arrays of same chars, and compared them using compare() method. As per the result, the comparison is printed.

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initialize first char array char array1[] = < 'a', 'b', 'd', 'f', 'p' >; // initialize second char array char array2[] = < 'a', 'b', 'd', 'f', 'p' >; int result = Arrays.compare(array1, array2); if(result > 0) < System.out.println("First array is greater than second array."); >else if (result == 0) < System.out.println("Arrays are same."); >else < System.out.println("First array is less than second array."); >> >

Output

Let us compile and run the above program, this will produce the following result −

Example 2

The following example shows the usage of Java Arrays compare​(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) method. First, we’ve created two arrays of different chars, and compared them using compare() method. As per the result, the comparison is printed.

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initialize first char array char array1[] = < 'a', 'b', 'd', 'f', 'p' >; // initialize second char array char array2[] = < 'a', 'x', 'd', 'f', 'p' >; int result = Arrays.compare(array1, 0, 2, array2, 0, 2); if(result > 0) < System.out.println("First array is greater than second array."); >else if (result == 0) < System.out.println("Arrays are same."); >else < System.out.println("First array is less than second array."); >> >

Output

Let us compile and run the above program, this will produce the following result −

First array is less than second array.

Example 3

The following example shows the usage of Java Arrays compare(char[], char[]) method. First, we’ve created two arrays of different chars, and compared them using compare() method. As per the result, the comparison is printed.

package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initialize first char array char array1[] = < 'a', 'x', 'd', 'f', 'p' >; // initialize second char array char array2[] = < 'a', 'b', 'd', 'f', 'p' >; int result = Arrays.compare(array1, array2); if(result > 0) < System.out.println("First array is greater than second array."); >else if (result == 0) < System.out.println("Arrays are same."); >else < System.out.println("First array is less than second array."); >> >

Output

Let us compile and run the above program, this will produce the following result −

First array is greater than second array.

Источник

Оцените статью