Guava-shorts
グアバ-ショートパンツクラス
Shortsは、プリミティブ型shortのユーティリティクラスです。
クラス宣言
以下は、 com.google.common.primitives.Shorts クラスの宣言です-
@GwtCompatible
public final class Shorts
extends Object
フィールド
Sr.No | Field & Description |
---|---|
1 |
static int BYTES プリミティブなshort値を表すのに必要なバイト数。 |
2 |
static short MAX_POWER_OF_TWO ショートとして表現できる最大の2のべき乗。 |
方法
Sr.No | Method & Description |
---|---|
1 |
static List<Short> asList(short… backingArray) Arrays.asList(Object [])と同様に、指定された配列に連動する固定サイズのリストを返します。 |
2 |
static short checkedCast(long value) 可能な場合、valueと等しいshort値を返します。 |
3 |
static int compare(short a, short b) 指定された2つの短い値を比較します。 |
4 |
static short[] concat(short[]… arrays) 提供された各配列から単一の配列に結合された値を返します。 |
5 |
static boolean contains(short[] array, short target) ターゲットが配列内のどこかの要素として存在する場合、trueを返します。 |
6 |
static short[] ensureCapacity(short[] array, int minLength, int padding) 配列と同じ値を含む配列を返しますが、指定された最小長であることが保証されています。 |
7 |
static short fromByteArray(byte[] bytes) ビッグエンディアン表現が最初の2バイトに格納されているshort値を返します。 ByteBuffer.wrap(bytes).getShort()と同等です。 |
8 |
static short fromBytes(byte b1, byte b2) バイト表現が指定された2バイトであるshort値をビッグエンディアン順に返します。 Shorts.fromByteArray(new byte [] \ {b1、b2})と同等です。 |
9 |
static int hashCode(short value) 値のハッシュコードを返します。呼び出しの結果と等しい((Short)value).hashCode()。 |
10 |
static int indexOf(short[] array, short target) 配列内の値ターゲットの最初の外観のインデックスを返します。 |
11 |
static int indexOf(short[] array, short[] target) 配列内の指定されたターゲットの最初のオカレンスの開始位置を返します。そのようなオカレンスがない場合は-1を返します。 |
12 |
static String join(String separator, short… array) 区切り文字で区切られた指定の短い値を含む文字列を返します。 |
13 |
static int lastIndexOf(short[] array, short target) 配列内の値ターゲットの最後の外観のインデックスを返します。 |
14 |
static Comparator<short[]> lexicographicalComparator() 2つの短い配列を辞書式に比較するコンパレーターを返します。 |
15 |
static short max(short… array) 配列に存在する最大値を返します。 |
16 |
static short min(short… array) 配列に存在する最小値を返します。 |
17 |
static short saturatedCast(long value) 値から値への最短距離を返します。 |
18 |
static Converter<String,Short> stringConverter() Short.decode(java.lang.String)とShort.toString()を使用して文字列とショートを変換するシリアル化可能なコンバーターオブジェクトを返します。 |
19 |
static short[] toArray(Collection<? extends Number> collection) コレクションの各値を含む配列を返し、Number.shortValue()の方法で短い値に変換します。 |
20 |
static byte[] toByteArray(short value) 2要素のバイト配列で値のビッグエンディアン表現を返します。 ByteBuffer.allocate(2).putShort(value).array()と同等です。 |
継承されるメソッド
このクラスは、次のクラスからメソッドを継承します-
- java.lang.Object
Shortsクラスの例
たとえば、* C:/> Guava。*で選択したエディターを使用して、次のJavaプログラムを作成します。
GuavaTester.java
import java.util.List;
import com.google.common.primitives.Shorts;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testShorts();
}
private void testShorts() {
short[] shortArray = {1,2,3,4,5,6,7,8,9};
//convert array of primitives to array of objects
List<Short> objectArray = Shorts.asList(shortArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
shortArray = Shorts.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< shortArray.length ; i++) {
System.out.print(shortArray[i] + " ");
}
System.out.println("]");
short data = 5;
//check if element is present in the list of primitives or not
System.out.println("5 is in list? " + Shorts.contains(shortArray, data));
//Returns the minimum
System.out.println("Min: " + Shorts.min(shortArray));
//Returns the maximum
System.out.println("Max: " + Shorts.max(shortArray));
data = 2400;
//get the byte array from an integer
byte[] byteArray = Shorts.toByteArray(data);
for(int i = 0; i< byteArray.length ; i++) {
System.out.print(byteArray[i] + " ");
}
}
}
結果を確認する
次のように javac コンパイラを使用してクラスをコンパイルします-
C:\Guava>javac GuavaTester.java
GuavaTesterを実行して結果を確認します。
C:\Guava>java GuavaTester
結果をご覧ください。
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[ 1 2 3 4 5 6 7 8 9 ]
5 is in list? true
Min: 1
Max: 9
9 96