本文整理汇总了Java中java.util.Arrays.binarySearch方法的典型用法代码示例。如果您正苦于以下问题:Java Arrays.binarySearch方法的具体用法?Java Arrays.binarySearch怎么用?Java Arrays.binarySearch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Arrays
的用法示例。
在下文中一共展示了Arrays.binarySearch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: nearest
import java.util.Arrays; //导入方法依赖的package包/类
public int nearest (int line, boolean backward) {
if (isEmpty()) {
return -1;
}
if (last == 0) {
return keys[last];
}
if (line < keys[0]) {
return backward ? keys[last] : keys[0];
}
if (line > keys[last]) {
return backward ? keys[last] : keys[0];
}
int idx = Arrays.binarySearch(keys, line);
if (idx < 0) {
idx = -idx + (backward ? -2 :- 1);
if (idx > last) {
idx = backward ? last : 0;
} else if (idx < 0) {
idx = backward ? last : 0;
}
}
return keys[idx];
}
示例2: retainAll
import java.util.Arrays; //导入方法依赖的package包/类
/** {@inheritDoc} */
public boolean retainAll( int[] array ) {
boolean changed = false;
Arrays.sort( array );
int[] set = _set;
byte[] states = _states;
_autoCompactTemporaryDisable = true;
for ( int i = set.length; i-- > 0; ) {
if ( states[i] == FULL && ( Arrays.binarySearch( array, set[i] ) < 0) ) {
removeAt( i );
changed = true;
}
}
_autoCompactTemporaryDisable = false;
return changed;
}
示例3: getDx
import java.util.Arrays; //导入方法依赖的package包/类
/**
* Returns the first derivation at x.
*
* @param x
* @return the first derivation at x
*/
public double getDx(double x) {
if (len.length == 0 || len.length == 1) {
return 0;
}
int index = Arrays.binarySearch(len, x);
if (index < 0) {
index = -(index + 1) - 1;
}
return b[index] + 2 * c[index] * (x - len[index]) + 3 * d[index] * Math.pow(x - len[index], 2);
}
示例4: forNamePrimitive
import java.util.Arrays; //导入方法依赖的package包/类
protected static Class forNamePrimitive(String name) {
if (name.length() <= 8) {
int p = Arrays.binarySearch(PRIMITIVE_NAMES, name);
if (p >= 0) {
return PRIMITIVES[p];
}
}
return null;
}
示例5: hasRole
import java.util.Arrays; //导入方法依赖的package包/类
/**
* Does the user represented by this Principal possess the specified role?
*
* @param role Role to be tested
*/
public boolean hasRole(String role) {
if("*".equals(role)) // Special 2.4 role meaning everyone
return true;
if (role == null)
return (false);
return (Arrays.binarySearch(roles, role) >= 0);
}
示例6: printAll
import java.util.Arrays; //导入方法依赖的package包/类
public static void printAll(int[] a) {
int N = a.length;
Arrays.sort(a);
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
int k = Arrays.binarySearch(a, -(a[i] + a[j]));
if (k > j) StdOut.println(a[i] + " " + a[j] + " " + a[k]);
}
}
}
示例7: count
import java.util.Arrays; //导入方法依赖的package包/类
public static int count(int[] a) {
int N = a.length;
Arrays.sort(a);
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
int k = Arrays.binarySearch(a, -(a[i] + a[j]));
if (k > j) cnt++;
}
}
return cnt;
}
示例8: getFrameForTimestampMs
import java.util.Arrays; //导入方法依赖的package包/类
/**
* Gets the frame index for specified timestamp.
*
* @param frameTimestampsMs an array of timestamps generated by {@link #getFrameForTimestampMs)}
* @param timestampMs the timestamp
* @return the frame index for the timestamp or the last frame number if the timestamp is outside
* the duration of the entire animation
*/
public int getFrameForTimestampMs(int frameTimestampsMs[], int timestampMs) {
int index = Arrays.binarySearch(frameTimestampsMs, timestampMs);
if (index < 0) {
return -index - 1 - 1;
} else {
return index;
}
}
示例9: commandStartIndex
import java.util.Arrays; //导入方法依赖的package包/类
@Override
int commandStartIndex() {
int start = Arrays.binarySearch(mCommandMaxBottom, mClippingRect.left);
// We don't care whether we matched or not, but positive indices are helpful. The binary search
// returns ~index in the case that it isn't a match, so reverse that here.
return start < 0 ? ~start : start;
}
示例10: setZoom
import java.util.Arrays; //导入方法依赖的package包/类
public void setZoom(double z) {
if (z <= 0.0) {
// This should never happen, it's just a kludge to make sure that
// we continue having valid data even if our caller is wrong.
z = Double.MIN_VALUE;
}
cur = Arrays.binarySearch(levels, z);
if (cur < 0) {
// if z is not a level, set cur to the next level > z
cur = -cur-1;
// check whether we are close to a level
if (cur < levels.length && Math.abs(z - levels[cur]) < 0.005) {
custom = -1;
}
else if (cur > 0 && Math.abs(z - levels[cur-1]) < 0.005) {
--cur;
custom = -1;
}
else {
custom = z;
}
}
else {
// custom is negative when we are in a predefined zoom level
custom = -1;
}
}
示例11: nextEntry
import java.util.Arrays; //导入方法依赖的package包/类
/**
* Get the key which follows the passed key, or -1. Will wrap around 0.
*/
public int nextEntry (int entry) {
int result = -1;
if (!isEmpty()) {
int idx = Arrays.binarySearch (keys, entry);
if (idx >= 0) {
result = idx == keys.length -1 ? keys[0] : keys[idx+1];
}
}
return result;
}
示例12: doTestEntityOrdering
import java.util.Arrays; //导入方法依赖的package包/类
private void doTestEntityOrdering(boolean computeInsertionPoint) throws Exception {
Entity e = new Entity(MacAddress.of(10L), VlanVid.ZERO, IPv4Address.NONE, IPv6Address.NONE, DatapathId.NONE, OFPort.ZERO, Entity.NO_DATE);
IEntityClass ec = createNiceMock(IEntityClass.class);
Device d = new Device(deviceManager, 1L, e, ec);
int expectedLength = 1;
Long[] macs = new Long[] { 5L, // new first element
15L, // new last element
7L, // insert in middle
12L, // insert in middle
6L, // insert at idx 1
14L, // insert at idx length-2
1L,
20L
};
for (Long mac: macs) {
e = new Entity(MacAddress.of(mac), VlanVid.ZERO, IPv4Address.NONE, IPv6Address.NONE, DatapathId.NONE, OFPort.ZERO, Entity.NO_DATE);
int insertionPoint;
if (computeInsertionPoint) {
insertionPoint = -(Arrays.binarySearch(d.entities, e)+1);
} else {
insertionPoint = -1;
}
d = deviceManager.allocateDevice(d, e, insertionPoint);
expectedLength++;
assertEquals(expectedLength, d.entities.length);
for (int i = 0; i < d.entities.length-1; i++)
assertEquals(-1, d.entities[i].compareTo(d.entities[i+1]));
}
}
示例13: getOffset
import java.util.Arrays; //导入方法依赖的package包/类
/**
* Gets the offset applicable at the specified instant in these rules.
* <p>
* The mapping from an instant to an offset is simple, there is only
* one valid offset for each instant.
* This method returns that offset.
*
* @param instant the instant to find the offset for, not null, but null
* may be ignored if the rules have a single offset for all instants
* @return the offset, not null
*/
public ZoneOffset getOffset(Instant instant) {
if (savingsInstantTransitions.length == 0) {
return standardOffsets[0];
}
long epochSec = instant.getEpochSecond();
// check if using last rules
if (lastRules.length > 0 &&
epochSec > savingsInstantTransitions[savingsInstantTransitions.length - 1]) {
int year = findYear(epochSec, wallOffsets[wallOffsets.length - 1]);
ZoneOffsetTransition[] transArray = findTransitionArray(year);
ZoneOffsetTransition trans = null;
for (int i = 0; i < transArray.length; i++) {
trans = transArray[i];
if (epochSec < trans.toEpochSecond()) {
return trans.getOffsetBefore();
}
}
return trans.getOffsetAfter();
}
// using historic rules
int index = Arrays.binarySearch(savingsInstantTransitions, epochSec);
if (index < 0) {
// switch negative insert position to start of matched range
index = -index - 2;
}
return wallOffsets[index + 1];
}
示例14: binarySearch
import java.util.Arrays; //导入方法依赖的package包/类
@Override
public final int binarySearch(int start, int end, Long value) {
return Arrays.binarySearch(values, start, end, value);
}
示例15: adjacent
import java.util.Arrays; //导入方法依赖的package包/类
private int adjacent(int n, int bias) {
int hit = Arrays.binarySearch(breaks, n);
int offset = (hit < 0 ? (bias < 0 ? -1 : -2) : 0);
return checkhit(Math.abs(hit) + bias + offset);
}