$start = Array(25000, 30000, 40000, 15900, 46000); $end = Array(45000, 47000, 60000, 55000, 85000); $cnt2 = 0; foreach($start as $val) { $cnt = 0; for($i=0;$i<count($start); $i++) { if($start[$i] <= $val && $val < $end[$i]) $cnt++; if($cnt == count($start)) $start_budget = $val; elseif($cnt >$cnt2) { $cnt2 = $cnt; $start_budget = $val; } } if($cnt2 == 0) $cnt2 = $cnt; } $cnt2 = 0; foreach($end as $val) { $cnt = 0; for($i=0;$i<count($end); $i++) { if($start[$i] < $val && $val <= $end[$i]) $cnt++; if($cnt == count($end)) $end_budget = $val; elseif($cnt > $cnt2) { $cnt2 = $cnt; $end_budget = $val; } if($cnt2 == 0) $cnt2 = $cnt; } } echo $start_budget; echo "<br/>"; echo $end_budget;
I have a Two PHP arrays as Below
Array 1 – Budget Start
$start = Array(25000,30000,35000,15900);
Array 2 – Budget End
$end = Array(40000,50000,60000,55000);
I want to Filter the Budget range which the user is actually looking for.For the above the budget range is 35000 For Budget Start and 40000 for Budget End.
Budget Start 35000 because
25000 <= 35000 < 40000
30000 <= 35000 < 50000
35000 <= 35000 < 60000
15900 <= 35000 < 55000
Budget End 40000 because
25000 < 40000 <= 40000
30000 < 40000 <= 50000
35000 < 40000 <= 60000
15900 < 40000 <= 55000
$start = Array(25000,30000,35000,15900); $end = Array(40000,50000,60000,55000); foreach($start as $val){ $cnt = 0; for($i=0;$i<count($start); $i++){ if($start[$i] <= $val && $val < $end[$i]){ $cnt++; } if($cnt == count($start)){ $start_budget = $val; } } } foreach($end as $val){ $cnt = 0; for($i=0;$i<count($end); $i++){ if($start[$i] < $val && $val <= $end[$i]){ $cnt++; } if($cnt == count($end)){ $end_budget = $val; } } } echo $start_budget; echo " "; echo $end_budget;
Static call from one package to another
package packA; public class sam1 { public static final double DIAMETER = 12756.32; // kilometers public static void hithere() { System.out.println("Hi There"); } } package packB; import packA.sam1; public class sam2 { public static void main(String args[]) { sam2 objsam2 = new sam2(); objsam2.halfway(); } public void halfway() { sam1.hithere(); System.out.println(sam1.DIAMETER/2.0); } }
-Two Packages – packA and packB – While importing static from packA to packB you should either use import packA.sam1; or import static packA.sam1.*;
Using import static packA.sam1; will not allow to access elements in package
If you use import packA.sam1;
className.StaticVariableName
If you use import static packA.sam1.*;
StaticVariableName
casting subclass to superclass java
The below code generates no compilation error in eclipse but throws error during Runtime.
public class Animal { public void eat(){} } public class Dog extends Animal { public void eat(){} public void main(String[] args) { Animal animal=new Animal(); Dog dog=(Dog) animal; } }
Output
Exception in thread "main" java.lang.ClassCastException: com.mugil.wild.Animal cannot be cast to com.mugil.wild.Dog at com.mugil.wild.Dog.main(Dog.java:12)
By using a cast you’re essentially telling the compiler “trust me. I’m a professional, I know what I’m doing and I know that although you can’t guarantee it, I’m telling you that this animal variable is definitely going to be a dog
Because you’re essentially just stopping the compiler from complaining, every time you cast it’s important to check that you won’t cause a ClassCastException by using instanceof in an if statement.
Generally, downcasting is not a good idea. You should avoid it. If you use it, you better include a check:
Animal animal = new Dog();
if (animal instanceof Dog)
{
Dog dog = (Dog) animal;
}
parent child class object relation
public class ClassA { public void MethodA() { System.out.println("This is Method A"); } } public class ClassB extends ClassA { public void MethodB() { System.out.println("I am Method in Class B"); } } public class ClassC { public static void main(String[] args) { ClassA objClassA1 = new ClassA(); ClassB objClassB2 = new ClassB(); //Child Class of Parent Type can be Created ClassA objClassB1 = new ClassB(); //Assigning a Parent class Type to Child Class is Not Allowed //Casting Should be Carried out ClassB objClassA2 = (ClassB) new ClassA(); objClassA1.MethodA(); objClassB2.MethodA(); objClassB2.MethodB(); objClassB1.MethodA(); } }
how to put giraffe in fridge
The following short quiz consists of 4 questions and will tell you whether you are qualified to be a professional. The questions are NOT that difficult.
1. How do you put a giraffe into a refrigerator?
Correct Answer: Open the refrigerator, put in the giraffe, and close the door. This question tests whether you tend to do simple things in an overly complicated way.
2. How do you put an elephant into a refrigerator?
Did you say, Open the refrigerator, put in the elephant, and close the refrigerator?
Wrong Answer.
Correct Answer: Open the refrigerator, take out the giraffe, put in the elephant and close the door. This tests your ability to think through the repercussions of your previous actions.
3. The Lion King is hosting an animal conference. All the animals attend…. except one. Which animal does not attend?
Correct Answer: The Elephant. The elephant is in the refrigerator. You just put him in there. This tests your memory.
Okay, even if you did not answer the first three questions correctly, you still have one more chance to show your true abilities.
4. There is a river you must cross but it is used by crocodiles, and you do not have a boat. How do you manage it?
Correct Answer: You jump into the river and swim across. Have you not been listening? All the crocodiles are attending the Animal Meeting. This tests whether you learn quickly from your mistakes.
when to use string builder, string buffer
In java String Builder Should be Used in case you need to perform concatenate more string together.
i.e
public String toString() { return a + b + c ; }For the above code using + will be converted toa = new StringBuilder() .append(a).append(b).append(c) .toString();For the above case you can use concat as below but since + will be converted as String Builder its better to use + rather than concat.
public String toString() { return a.concat(b).concat(c); }The key is whether you are writing a single concatenation all in one place or accumulating it over time.
There's no point in explicitly using StringBuilder.
But if you are building a string e.g. inside a loop, use StringBuilder.
To clarify, assuming that hugeArray contains thousands of strings, code like this:
... String result = ""; for (String s : hugeArray) { result = result + s; }It should be as below
... StringBuilder sb = new StringBuilder(); for (String s : hugeArray) { sb.append(s); } String result = sb.toString();
How to create for loop in MySQL Procedure
DROP PROCEDURE IF EXISTS while1; CREATE PROCEDURE while1() BEGIN DECLARE v_max INT DEFAULT 15; DECLARE v_counter INT DEFAULT 0; WHILE v_counter < v_max do SELECT v_counter; SET v_counter=v_counter+1; END WHILE; END;
how to sort array by inner array php
$arrSam1[0] = array("a", array("2")); $arrSam1[1] = array("b", array("1","2", "3", "4")); $arrSam1[2] = array("c", array("1","2", "3")); $arrSam = usort($arrSam1, 'cmp'); function cmp($a, $b) { return count($a[1]) > count($b[1]) ? -1 : 1; }
Find common elements in two arrays javascript
Common elements in two arrays javascript
arrNum1 = new Array(1,2,3); arrNum2 = new Array(2,3,4,5); arrNum3 = new Array(); arrNum3 = intersect_safe(arrNum1, arrNum2); for(i=0;i<arrNum3.length;i++) alert(arrNum3[i]); function intersect_safe(a, b) { var ai=0, bi=0; var result = new Array(); while(ai<a.length && bi < b.length ) { if(a[ai] != b[bi]) { bi++; } else { result.push(a[ai]); ai++; bi++; } } return result; }