Thứ Sáu, 22 tháng 4, 2016

Comparator interface trong java

Để cung cấp phương thức so sánh hai đối tượng với nhau, java cung cấp cho ta interface comparator. Interface này bắt ta phải cài đặt hàm compare trả lại 1 giá trị
VD: compare(A,B) = 0 <==> A = B
compare(A,B) < 0 <==> A < B
compare(A,B) > 0 <==> A > B.
Ta sẽ hiểu rõ hơn với code bằng java sau

// Java program to demonstrate working of Comparator
// interface
import java.util.*;
import java.lang.*;
import java.io.*;

// A class to represent a student.
class Student
{
    int rollno;
    String name, address;

    // Constructor
    public Student(int rollno, String name,
                               String address)
    {
        this.rollno = rollno;
        this.name = name;
        this.address = address;
    }

    // Used to print student details in main()
    public String toString()
    {
        return this.rollno + " " + this.name +
                           " " + this.address;
    }
}

class Sortbyroll implements Comparator<Student>
{
    public int compare(Student a, Student b)
    {
        // return < 0 if we want a to come before b
        // in soreted order,
        // > 0 if a comes after b,
        // = 0 if a == b
        return a.rollno - b.rollno;
    }
}

class Sortbyname implements Comparator<Student>
{
    public int compare(Student a, Student b)
    {
        // <0 if we want a to come before b in sorted
        // order,
        // >0 if a comes after b,
        // =0 if a==b
        return a.name.compareTo(b.name);
    }
}

// Driver class
class Main
{
    public static void main (String[] args)
    {
        ArrayList<Student> ar = new ArrayList<Student>();
        ar.add(new Student(111, "bbbb", "london"));
        ar.add(new Student(131, "aaaa", "nyc"));
        ar.add(new Student(121, "cccc", "jaipur"));

        System.out.println("Unsorted");
        for (int i=0; i<ar.size(); i++)
            System.out.println(ar.get(i));

        Collections.sort(ar, new Sortbyroll());

        System.out.println("\nSorted by rollno");
        for (int i=0; i<ar.size(); i++)
            System.out.println(ar.get(i));

        Collections.sort(ar, new Sortbyname());

        System.out.println("\nSorted by name");
        for (int i=0; i<ar.size(); i++)
            System.out.println(ar.get(i));
    }
}

Thứ Tư, 20 tháng 4, 2016

Làm thế nào để đảo chuỗi nhanh trong c++

Có 2 cách:
- Cách 1: Tự viét hàm
// A Simple C++ program to reverse a string
#include<bits/stdc++.h>
using namespace std;

// Function to reverse a string
void reverseStr(string &str)
{
    int n = str.length();

    // Swap character starting from two
    // corners
    for (int i=0; i<n/2; i++)
       swap(str[i], str[n-i-1]);
}

// Driver program
int main()
{
   string str = "geeksforgeeks";
   reverseStr(str);
   cout << str;
   return 0;
}
- Cách 2: Dùng hàm có sẵn:
// A quickly written program for reversing a string
// using reverse()
#include<bits/stdc++.h>
using namespace std;
int main()
{
   string str = "geeksforgeeks";
   
   // Reverse str[beign..end]
   reverse(str.begin(),str.end());
   
   cout << str;
   return 0;
}

Interface trong class

Cần chú ý rằng interface khi khai báo trong class thì có thể là default, public, private, protected. Không giống khi ta khai báo 1 interface độc lập thì chỉ có thể là public


// Java program to demonstrate working of
// interface inside a class.
import java.util.*;
class Test
{
    interface Yes
    {
        void show();
    }
}
 
class Testing implements Test.Yes
{
    public void show()
    {
        System.out.println("show method of interface");
    }
}
 
class A
{
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj=t;
        obj.show();
    }
}

// Java program to demonstrate protected
// specifier for nested interface.
import java.util.*;
class Test
{
    protected interface Yes
    {
        void show();
    }
}
 
class Testing implements Test.Yes
{
    public void show()
    {
        System.out.println("show method of interface");
    }
}
 
class A
{
    public static void main(String[] args)
    {
        Test.Yes obj;
        Testing t = new Testing();
        obj=t;
        obj.show();
    }
}

Copyright © Dịch từ nguồn geeksforgeeks.org

Distributed By My Blogger Themes | Blogger Theme By NewBloggerThemes Up ↑