Monday, October 22, 2012

Vector Demo in Java

Program for vector in java.

Below is the program.

import java.util.*;

class vectorDemo
{
    public static void main(String args[])
    {
        Vector v = new Vector(3,2);

        System.out.println("Intial Size : " + v.size());
        System.out.println("Intial Capacity : " + v.capacity());

        v.addElement(new Integer(1));
        v.addElement(new Integer(2));
        v.addElement(new Integer(3));
        v.addElement(new Integer(4));

        System.out.println("Capacity after 4 additions : " + v.capacity());
        v.addElement(new Double(4.55));
        System.out.println("Capacity after adding double element : " + v.capacity());       
    }
}

Array Keys and Array Values

Below is Simple Example which shows how the array keys or array values can be obtained from a array.


<?php

     $personinfo=array(array("name"=>"john","address"=>"pune","age"=>30),
          array("name"=>"Sathya","address"=>"Nashik","age"=>39),
          array("name"=>"madhav","address"=>"pune","age"=>22));

      $arr1=array_keys($personinfo);

      $arr2=array_values($personinfo);

       echo "OUTPUT AS ONLY KEYS FOR GIVEN ARRAY";
       echo "<br><br>";
       print_r($arr1);

      echo "<br><br>";   
      echo "OUTPUT AS ONLY VALUES FOR GIVEN ARRAY";
      echo "<br><br>";
      print_r($arr2);

?>

PHP Function to create slug from string

Here is simple Function to create slug from string.



function getSlug($title)
             {
                        $slug = preg_replace("/[^a-zA-Z0-9 ]/", "", $title);
                           $slug = str_replace(" ", "-", $slug);
                        $slug=strtolower($slug);
                        return($slug); 
  }

PHP Function to generate random unique string.

Here is a Simple Function to generate random unique string.



            function GenerateNumber($min, $max)
{
                        // Create the meta-password
                        $sMetaPassword = "";
                        global $CONFIG;
                        $ahPasswordGenerator = array(
                        "C" => array('characters' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'minimum' => $min, 'maximum' => $max),
//"S" => array('characters' => "!@()-_=+?*^&", 'minimum' => 2, 'maximum' => 2
"N" => array('characters' => '1234567890', 'minimum' => $min, 'maximum' => $max)
);
                        foreach ($ahPasswordGenerator as $cToken => $ahPasswordSeed)
$sMetaPassword .= str_repeat($cToken, rand($ahPasswordSeed['minimum'], $ahPasswordSeed['maximum']));
                                    $sMetaPassword = str_shuffle($sMetaPassword);
                        // Create the real password
                        $arBuffer = array();
                        for ($i = 0; $i < strlen($sMetaPassword); $i ++)
$arBuffer[] = $ahPasswordGenerator[(string)$sMetaPassword[$i]]['characters'][rand(0, strlen($ahPasswordGenerator[$sMetaPassword[$i]]['characters']) - 1)];
                        return implode("", $arBuffer);

PHP Function to check whether the given number is decimal or not.

Below is a simple Function to check whether the given number is decimal or not.



function check_decimal($dec_val, $decimals = 2)
{
                        $pattern = "/^[-]*[0-9][0-9]*\.[0-9]{".$decimals."}$/";
                        if (preg_match($pattern,  $dec_val))
                                    return true;
                        else
                                    return false;
}

PHP Function to check whether a given date is valid or not.

Here is a Simple Function to check whether a given date is valid or not.



           function is_valid_date($value, $separator='-')
{
              if(strlen($value) <= 10)
                        {
                        // check date
                        $arr=explode($separator,$value);
                        $day=$arr[2];
                        $month=$arr[1];
                        $year=$arr[0];
                        if(@checkdate($month, $day, $year))
                                    return true;
              }
             return false;
}

PHP Function to calculate days difference between given 2 dates.

Here is a simple  Function to calculate days difference between given 2 dates.



function daysDifference($endDate, $beginDate)
{

              //explode the date by "-" and storing to array
             $date_parts1=explode("-", $beginDate);
             $date_parts2=explode("-", $endDate);
             //gregoriantojd() Converts a Gregorian date to Julian Day Count
            $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
             $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
             return $end_date - $start_date;
}

PHP Function to convert date from mm/dd/yyyy to yyyy-mm-dd

Here is a simple Function to convert date from  mm/dd/yyyy to yyyy-mm-dd.



function convert_date($date)
{
                        if($date=='')
                        return '';
                     else
                        $dateArray=explode('/',$date);

                        return "$dateArray[2]-$dateArray[0]-$dateArray[1]";
}

PHP Function to validate an email address

Here is a simple Function to validate an email address in php.



function check_email($mail_address)
{
                        if($mail_address == '')
                        {
                                    return false;
                        }
                        else
                        {
if (preg_match("/^[0-9a-z]+(([\.\-_])[0-9a-z]+)*@[0-9a-z]+(([\.\-])[0-9a-z-]+)*\.[a-z]{2, 4}$/i",  strtolower($mail_address)))
                                    return true;
                        else
                                    return false;
                        }
}

PHP Function to get extension of a file.

Here is a simple function which returns the extension of the file.



           function getExtension($str)
{
                        $i = strrpos($str,".");
                        if (!$i) { return ""; }
                        $l = strlen($str) - $i;
                        $ext = substr($str,$i+1,$l);
                        return $ext;

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...