Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

what about this pattern: 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13 without matrix in java

what about this pattern: without matrix in  java 

1 2 3 4  
8 7 6 5  
9 10 11 12  
16 15 14 13

class pattern

    static void printPattern(int n)
    {
int count = 1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i%2!=0)
{
System.out.print(count++);
}
else
{
System.out.print(count--);
}
}
if(i%2!=0)
{
count = count+3;
}
else
{
count = count+5;
}
System.out.println();
}   
}
    public static void main(String[] args) 
    {
        printPattern(4);      
    }

}



Mobile Number Pattern matches In Java



A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.
A typical invocation sequence is thus

 Pattern p = Pattern.compile("a*b");
 Matcher m = p.matcher("aaaaab");
 boolean b = m.matches();



 Example

import java.util.regex.*;  
class Demo
{
 public static void main(String[] args)
 {
  String  p1="[7-9]{1}[0-9]{9}";
  Pattern p = Pattern.compile(p1);
  Matcher m = p.matcher("9909556848");
  
  if( m.matches())
  {
   System.out.println("Match");
  }
  else
  {
   System.out.println("Not Match");
  }
 }
}


Out Put


compare and compareTo In JAVA Programming

What is the difference between compare() and compareTo()?





compareTo() is from the Comparable interface.
compare() is from the Comparator interface.
Both methods do the same thing, but each interface is used in a slightly different context.
The Comparable interface is used to impose a natural ordering on the objects of the implementing class. The compareTo() method is called the natural comparison method. The Comparator interface is used to impose a total ordering on the objects of the implementing class. For more information, see the links for exactly when to use each interface.




java.lang.Comparable
1. To implements comparable interface, class must implement a single method compareTo()
int a.compareTo(b)
2. You must modify the class whose instance you want to sort. So that only one sort sequence can be created per class.
java.util.Comparator
1. To implements comparator interface, class must implement a single method compare()
int compare (a,b)
2. You build a class separate from class whose instance you want to sort. So that multiple sort sequence can be created per class.


Example 



Comparable
import java.util.*;
class A implements Comparable
{
          int no;
          String name;
          A(int a,String n)
          {
                   this.no=a;
                   this.name=n;
          }
          int getVal()
          {
                   return this.no;
          }
          String getName()
          {
                   return this.name;
          } 
          public String toString()
          {
                   return this.no+" - "+this.name;
          }

          public int compareTo(Object a1)
          {
                   //return this.no-((A)a1).no;   -- Sort Number
                    return (((A)a1).name).compareTo(this.name);  // Sort Name
          }
          public static void main(String []s)
          {
                    A a[] = new A[5];
                   a[0] = new A(3,"Vijay");
                   a[1] = new A(4,"Ajay");
                   a[2] = new A(1,"Sanjay");
                   a[3] = new A(2,"Jay");
                   a[4] = new A(5,"Parajay");
                            
                   Arrays.sort(a);

                   for(int i=0;i<5;i++)
                   {
                             System.out.println(a[i]);                   
                   }
          }
}                         

// Comparator


import java.util.*;
class Ano implements Comparator
{
          public int compare(Object o1,Object o2)
          {
                   return ((A)o1).no-((A)o2).no;  
          }                 
}
class Aname implements Comparator
{
          public int compare(Object o1,Object o2)
          {
                   return (((A)o1).name).compareTo(((A)o2).name);
          }                 
}
class A
{
          int no;
          String name;
          A(int a,String n)
          {
                   this.no=a;
                   this.name=n;
          }
           
          public String toString()
          {
                   return this.no+" - "+this.name;
          }
         
          public static void main(String []s)
          {
                   ArrayList a = new ArrayList();
                   a.add(new A(3,"Vijay"));
                   a.add(new A(4,"Ajay"));
                   a.add(new A(1,"Sanjay"));
                   a.add(new A(2,"Jay"));
                   a.add(new A(5,"Parajay"));
                  
                   Collections.sort(a,new Aname());

                   Iterator ir = a.iterator(); 
                   A a1;
                   while(ir.hasNext())
                   {
                             a1 = (A)ir.next();
                             System.out.println(a1); 
                   }
          }
}