当前位置:文档之家› ComponentSampleModel

ComponentSampleModel

ComponentSampleModel (Java 2 Platform SE 5.0)
function windowTitle()
{
parent.document.title="ComponentSampleModel (Java 2 Platform SE 5.0)";
}

概述 
软件包 
 类 
使用 
树 
已过时 
索引 
帮助 

JavaTM 2 PlatformStandard Ed. 5.0
 上一个类 
 下一个类
框架  
 无框架  
 
!--
if(window==top) {
document.writeln('所有类');
}
//--
所有类
摘要: 嵌套 | 字段 | 构造方法 | 方法
详细信息: 字段 | 构造方法 | 方法
java.awt.image
类 ComponentSampleModel
https://www.doczj.com/doc/fc15907065.html,ng.Object
java.awt.image.SampleModel
https://www.doczj.com/doc/fc15907065.html,ponentSampleModel
直接已知子类: BandedSampleModel, PixelInterleavedSampleModel
public class ComponentSampleModelextends SampleModel
此类表示按每个像素样本占用 DataBuffer 中一个数据元素的方式存储的图像数据。它可存储 N 个样本,这 N 个样本可通过 N 个独立的数据数组元素组成像素。不同的 band 可能位于 DataBuffer 的不同库中。由于提供了 Accessor 方法,因此可以直接对图像数据进行操作。此类可以支持不同类型的交错,如 band 交错、scanline 交错和像素交错等。像素跨度 (pixel stride) 是同一 scanline 上同一 band 的两个样本之间的数据数组元素数。Scanline 跨度是给定样本与下一 scanline 的同一列中对应样本之间的数据数组元素数。Band 偏移量表示从保持每个 band 的 DataBuffer 库的第一个数据数组元素到 band 的第一个样本之间的数据数组元素数。band 从 0 到 N-1 编号。此类可以表示这样的图像数据:每个样本都可以按 8、16 或 32 位存储无符号整数的数据(分别使用 DataBuffer.TYPE_BYTE、DataBuffer.TYPE_USHORT 或 DataBuffer.TYPE_INT)、每个样本都可以按 16 位存储有符号整数的数据(使用 DataBuffer.TYPE_SHORT),或者每个样本都是有符号 float 和 double 量的数据(分别使用 DataBuffer.TYPE_FLOAT 或 DataBuffer.TYPE_DOUBLE)。所有给定的 ComponentSampleModel 样本都是以同样的精度存储的。所有跨度和偏移量必须为非负数。此类支持 TYPE_BYTE、TYPE_USHORT、TYPE_SHORT、TYPE_INT、TYPE_FLOAT、TYPE_DOUBLE。
另请参见:PixelInterleavedSampleModel,
BandedSampleModel
字段摘要
protected  int[]
bandOffsets
          数据数组元素中所有 band 的偏移量。
protected  int[]
bankIndices
          存储图像数据 band 的各库的索引。
protected  int
numBands
          此 ComponentSampleModel

中的 band 数量。
protected  int
numBanks
          此 ComponentSampleModel 中的库数量。
protected  int
pixelStride
          由此 ComponentSampleModel 所描述的图像数据区域的像素跨度(以数据数组元素为单位)。
protected  int
scanlineStride
          由此 ComponentSampleModel 所描述的图像数据区域的行跨度(以数据数组元素为单位)。
 
从类 java.awt.image.SampleModel 继承的字段
dataType, height, width
 
构造方法摘要
ComponentSampleModel(int dataType,
int w,
int h,
int pixelStride,
int scanlineStride,
int[] bandOffsets)
          构造具有指定参数的 ComponentSampleModel。
ComponentSampleModel(int dataType,
int w,
int h,
int pixelStride,
int scanlineStride,
int[] bankIndices,
int[] bandOffsets)
          构造具有指定参数的 ComponentSampleModel。
 
方法摘要
 SampleModel
createCompatibleSampleModel(int w,
int h)
          创建具有指定宽度和高度的 ComponentSampleModel。
 DataBuffer
createDataBuffer()
          创建对应于此 ComponentSampleModel 的 DataBuffer。
 SampleModel
createSubsetSampleModel(int[] bands)
          创建具有此 ComponentSampleModel 的 band 子集的新 ComponentSampleModel。
 boolean
equals(Object o)
          指示某个其他对象是否与此对象“相等”。
 int[]
getBandOffsets()
          返回所有 band 的 band 偏移量。
 int[]
getBankIndices()
          返回所有 band 的库索引。
 Object
getDataElements(int x,
int y,
Object obj,
DataBuffer data)
          以 TransferType 类型的基本数组形式返回单个像素的数据。
 int
getNumDataElements()
          返回使用 getDataElements(int, int, Object, DataBuffer) 和 setDataElements(int, int, Object, DataBuffer) 方法传输像素

所需的数据元素的数量。
 int
getOffset(int x,
int y)
          获取像素 (x,y) 的第一个 band 的偏移量。
 int
getOffset(int x,
int y,
int b)
          获取像素 (x,y) 的 band b 的偏移量。
 int[]
getPixel(int x,
int y,
int[] iArray,
DataBuffer data)
          返回 int 数组中指定像素的所有样本(每个数组元素一个样本)。
 int[]
getPixels(int x,
int y,
int w,
int h,
int[] iArray,
DataBuffer data)
          返回 int 数组中像素的指定矩形区域的所有样本(每个数组元素一个样本)。
 int
getPixelStride()
          返回此 ComponentSampleModel 的像素跨度。
 int
getSample(int x,
int y,
int b,
DataBuffer data)
          以 int 类型返回位于 (x,y) 处像素的指定 band 中的样本。
 double
getSampleDouble(int x,
int y,
int b,
DataBuffer data)
          针对 (x,y) 处的像素,返回指定 band 中样本的 double 值。
 float
getSampleFloat(int x,
int y,
int b,
DataBuffer data)
          以 float 类型返回位于 (x,y) 处像素的指定 band 中的样本。
 int[]
getSamples(int x,
int y,
int w,
int h,
int b,
int[] iArray,
DataBuffer data)
          针对一个 int 数组中指定的像素矩形,返回指定 band 中的样本,每个数据数组元素对应一个样本。
 int[]
getSampleSize()
          返回所有 band 的每样本的位数。
 int
getSampleSize(int band)
          返回指定 band 的每样本位数。
 int
getScanlineStride()
          返回此 ComponentSampleModel 的 scanline 跨度。
 int
hashCode()
          返回该对象的哈希码值。
 void
setDataElements(int x,
int y,
Object obj,
DataBuffer data)
 &

nbsp;        从 TransferType 类型的基本数组设置指定 DataBuffer 中单个像素的数据。
 void
setPixel(int x,
int y,
int[] iArray,
DataBuffer data)
          使用输入样本的 int 数组设置 DataBuffer 中的像素。
 void
setPixels(int x,
int y,
int w,
int h,
int[] iArray,
DataBuffer data)
          从包含每个数组元素一个样本的 int 数组设置像素矩形区域的所有样本。
 void
setSample(int x,
int y,
int b,
double s,
DataBuffer data)
          使用输入的 double 值,为 DataBuffer 中 (x,y) 处像素设置指定 band 中的样本。
 void
setSample(int x,
int y,
int b,
float s,
DataBuffer data)
          使用输入的 float 值,为 DataBuffer 中 (x,y) 处像素设置指定 band 中的样本。
 void
setSample(int x,
int y,
int b,
int s,
DataBuffer data)
          使用输入的 int 值,为 DataBuffer 中 (x,y) 处的像素设置指定 band 中的样本。
 void
setSamples(int x,
int y,
int w,
int h,
int b,
int[] iArray,
DataBuffer data)
          根据为每个数据数组元素包含一个样本的 int 数组,设置指定像素矩形的指定 band 中的样本。
 
从类 java.awt.image.SampleModel 继承的方法
getDataElements, getDataType, getHeight, getNumBands, getPixel, getPixel, getPixels, getPixels, getSamples, getSamples, getTransferType, getWidth, setDataElements, setPixel, setPixel, setPixels, setPixels, setSamples, setSamples
 
从类 https://www.doczj.com/doc/fc15907065.html,ng.Object 继承的方法
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
字段详细信息
bandOffsets
protected int[] bandOffsets
数据数组元素中所有 band 的偏移量。
bankIndices
protected int[] bankIndices
存储图像数据 band 的各库的索引。
numBands
protected int numBands
此 ComponentSampleModel 中的 band 数量。
numBanks
protected int numBanks
此 ComponentSampleModel 中的库数量。
scanlineStride
protected int scanlineStride
由此 ComponentSampleModel 所描述的图像数据区域的行跨度(以数据数组元素为单位)。
pixelStride
protected int pixelStride
由此

ComponentSampleModel 所描述的图像数据区域的像素跨度(以数据数组元素为单位)。
构造方法详细信息
ComponentSampleModel
public ComponentSampleModel(int dataType,
int w,
int h,
int pixelStride,
int scanlineStride,
int[] bandOffsets)
构造具有指定参数的 ComponentSampleModel。band 数量将由 bandOffsets 数组的长度给定。所有 band 都将存储在 DataBuffer 的第一个库中。
参数:dataType - 存储样本的数据类型w - 描述的图像数据区域的宽度(以像素为单位)h - 描述的图像数据区域的高度(以像素为单位)pixelStride - 描述的图像数据区域的像素跨度scanlineStride - 描述的图像数据区域的行跨度bandOffsets - 所有 band 的偏移量
抛出:
IllegalArgumentException - 如果 w 或 h 不大于 0
IllegalArgumentException - 如果 pixelStride 小于 0
IllegalArgumentException - 如果 scanlineStride 小于 0
IllegalArgumentException - 如果 numBands 小于 1
IllegalArgumentException - 如果 w 和 h 的乘积大于 Integer.MAX_VALUE
IllegalArgumentException - 如果 dataType 不是受支持的数据类型之一
ComponentSampleModel
public ComponentSampleModel(int dataType,
int w,
int h,
int pixelStride,
int scanlineStride,
int[] bankIndices,
int[] bandOffsets)
构造具有指定参数的 ComponentSampleModel。band 数量将由 bandOffsets 数组的长度给定。不同的 band 可能存储在 DataBuffer 的不同库中。
参数:dataType - 存储样本的数据类型w - 描述的图像数据区域的宽度(以像素为单位)h - 描述的图像数据区域的高度(以像素为单位)pixelStride - 描述的图像数据区域的像素跨度scanlineStride - 描述的图像数据区域的行跨度bankIndices - 所有 band 的库索引bandOffsets - 所有 band 的 band 偏移量
抛出:
IllegalArgumentException - 如果 w 或 h 不大于 0
IllegalArgumentException - 如果 pixelStride 小于 0
IllegalArgumentException - 如果 scanlineStride 小于 0
IllegalArgumentException - 如果 bankIndices 的长度不等于 bankOffsets 的长度
IllegalArgumentException - 如果 bandIndices 的 band 索引中任何一项小于 0
IllegalArgumentException - 如果 dataType 不是受支持的数据类型之一
方法详细信息
createCompatibleSampleModel
public SampleModel createCompatibleSampleModel(int w,
int h)
创建具有指定宽度和高度的 ComponentSampleModel。新的 SampleModel 将有与此 SampleModel 相同的 band 数量、存储数据类型、交错方案

和像素跨度。
指定者:类 SampleModel 中的 createCompatibleSampleModel
参数:w - 得到的 SampleModel 的宽度h - 得到的 SampleModel 的高度
返回:具有指定大小的 ComponentSampleModel
抛出:
IllegalArgumentException - 如果 w 或 h 不大于 0
createSubsetSampleModel
public SampleModel createSubsetSampleModel(int[] bands)
创建具有此 ComponentSampleModel 的 band 子集的新 ComponentSampleModel。新的 ComponentSampleModel 可以与任何现有 ComponentSampleModel 可与之一起使用的 DataBuffer 一起使用。新的 ComponentSampleModel/DataBuffer 组合表示具有原 ComponentSampleModel/DataBuffer 组合的 band 子集的图像。
指定者:类 SampleModel 中的 createSubsetSampleModel
参数:bands - 此 ComponentSampleModel 的 band 的子集
返回:已创建的具有此 ComponentSampleModel 的 band 子集的 ComponentSampleModel。
createDataBuffer
public DataBuffer createDataBuffer()
创建对应于此 ComponentSampleModel 的 DataBuffer。DataBuffer 对象的数据类型、库的数量和大小与此 ComponentSampleModel 一致。
指定者:类 SampleModel 中的 createDataBuffer
返回:其数据类型、库的数量和大小与此 ComponentSampleModel 一致的 DataBuffer。
getOffset
public int getOffset(int x,
int y)
获取像素 (x,y) 的第一个 band 的偏移量。第一个 band 的样本可以从具有如同下例的 ComponentSampleModel csm 的 DataBuffer data 中检索

data.getElem(csm.getOffset(x, y));

参数:x, y - 像素的位置
返回:指定像素的第一个 band 的偏移量。
getOffset
public int getOffset(int x,
int y,
int b)
获取像素 (x,y) 的 band b 的偏移量。band b 的样本可以从具有如下示例的 ComponentSampleModel csm 的 DataBuffer data 中检索

data.getElem(csm.getOffset(x, y, b));

参数:x, y - 指定像素的位置b - 指定的 band
返回:指定像素的指定 band 的偏移量。
getSampleSize
public final int[] getSampleSize()
返回所有 band 的每样本的位数。
指定者:类 SampleModel 中的 getSampleSize
返回:包含所有 band 的每样本位数的数组,其中数组中的每个元素都表示一个 band。
getSampleSize
public final int getSampleSize(int band)
返回指定 band 的每样本位数。
指定者:类 SampleModel 中的 getSampleSize
参数:band - 指定的 band
返回:指定 band 的每样本位数。
getBankIndices
public final int[] getBankIndices()
返回所有 band 的库索引。
返回:所有 band 的库索引。
getBandOffsets
public final int[] getBandOffsets()
返回所有 band 的 band 偏移量。
返回:所有 band 的 band 偏移量。
getScanlineStride
public final int getScanlineStride()
返回此 ComponentSampleModel 的 scanline 跨度。

返回:此 ComponentSampleModel 的 scanline 跨度。
getPixelStride
public final int getPixelStride()
返回此 ComponentSampleModel 的像素跨度。
返回:此 ComponentSampleModel 的像素跨度。
getNumDataElements
public final int getNumDataElements()
返回使用 getDataElements(int, int, Object, DataBuffer) 和 setDataElements(int, int, Object, DataBuffer) 方法传输像素所需的数据元素的数量。对于 ComponentSampleModel,这与 band 的数量相同。
指定者:类 SampleModel 中的 getNumDataElements
返回:使用 getDataElements 和 setDataElements 方法传输像素所需的数据元素的数量。另请参见:SampleModel.getNumDataElements(),
SampleModel.getNumBands()
getDataElements
public Object getDataElements(int x,
int y,
Object obj,
DataBuffer data)
以 TransferType 类型的基本数组形式返回单个像素的数据。对于 ComponentSampleModel,这与数据类型相同,且每个数组元素返回一个样本。通常,obj 应该以 null 传入,以便自动创建 Object 和成为正确的基本数据类型。

以下代码说明了从 DataBuffer db1(其存储布局由 ComponentSampleModel csm1 描述)到 DataBuffer db2(其存储布局由 ComponentSampleModel csm2 描述)传输一个像素数据的情况。该传输通常比使用 getPixel 和 setPixel 更有效。

ComponentSampleModel csm1, csm2;
DataBufferInt db1, db2;
csm2.setDataElements(x, y,
csm1.getDataElements(x, y, null, db1), db2);

如果 SampleModel 对象具有相同数量的 band,相应 band 具有相同数量的每样本位数,并且 TransferType 相同,则使用 getDataElements 和 setDataElements 在两个 DataBuffer/SampleModel 对之间传输是合理的。

如果 obj 不为 null,则它应该为 TransferType 类型的基本数组。否则,将抛出 ClassCastException。如果坐标不在边界内部,或如果 obj 不为 null 且不是特别大,不足以保存像素数据,则可能抛出 ArrayIndexOutOfBoundsException。
指定者:类 SampleModel 中的 getDataElements
参数:x, y - 像素位置的坐标obj - 如果为非 null,则是一个要在其中返回像素数据的基本数组data - 包含图像数据的 DataBuffer
返回:指定像素的数据
抛出:
NullPointerException - 如果数据为 null。
ArrayIndexOutOfBoundsException - 如果坐标不在边界内部,或者 obj 太小不能保存输出。另请参见:setDataElements(int, int, Object, DataBuffer)
getPixel
public int[] getPixel(int x,
int y,
int[] iArray,
DataBuffer data)
返回 int 数组中指定像素的所有样本(每个数组元素一个样本)。如果坐标不在边界内部,则可能抛出 ArrayIn

dexOutOfBoundsException。
覆盖:类 SampleModel 中的 getPixel
参数:x, y - 像素位置的坐标iArray - 如果为非 null,则返回此数组中的样本data - 包含图像数据的 DataBuffer
返回:指定像素的样本。
抛出:
NullPointerException - 如果数据为 null。
ArrayIndexOutOfBoundsException - 如果坐标不在边界内部,或者 iArray 太小不能保存输出。另请参见:setPixel(int, int, int[], DataBuffer)
getPixels
public int[] getPixels(int x,
int y,
int w,
int h,
int[] iArray,
DataBuffer data)
返回 int 数组中像素的指定矩形区域的所有样本(每个数组元素一个样本)。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
覆盖:类 SampleModel 中的 getPixels
参数:x, y - 左上角像素位置的坐标w - 像素矩形区域的宽度h - 像素矩形区域的高度iArray - 如果为非 null,则返回此数组中的样本data - 包含图像数据的 DataBuffer
返回:指定区域内的像素的样本。另请参见:setPixels(int, int, int, int, int[], DataBuffer)
getSample
public int getSample(int x,
int y,
int b,
DataBuffer data)
以 int 类型返回位于 (x,y) 处像素的指定 band 中的样本。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
指定者:类 SampleModel 中的 getSample
参数:x, y - 像素位置的坐标b - 要返回的 banddata - 包含图像数据的 DataBuffer
返回:指定像素的指定 band 中的样本另请参见:setSample(int, int, int, int, DataBuffer)
getSampleFloat
public float getSampleFloat(int x,
int y,
int b,
DataBuffer data)
以 float 类型返回位于 (x,y) 处像素的指定 band 中的样本。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
覆盖:类 SampleModel 中的 getSampleFloat
参数:x, y - 像素位置的坐标b - 要返回的 banddata - 包含图像数据的 DataBuffer
返回:表示指定像素的指定 band 中样本的 float 值。
getSampleDouble
public double getSampleDouble(int x,
int y,
int b,
DataBuffer data)
针对 (x,y) 处的像素,返回指定 band 中样本的 double 值。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
覆盖:类 SampleModel 中的 getSampleDouble
参数:x, y - 像素位置的坐标b - 要返回的 banddata - 包含图像数据的 DataBuffer
返回:表示指定像素的指定 band 中样本的 double 值。
getSamples
pu

blic int[] getSamples(int x,
int y,
int w,
int h,
int b,
int[] iArray,
DataBuffer data)
针对一个 int 数组中指定的像素矩形,返回指定 band 中的样本,每个数据数组元素对应一个样本。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
覆盖:类 SampleModel 中的 getSamples
参数:x, y - 左上角像素位置的坐标w - 像素矩形区域的宽度h - 像素矩形区域的高度b - 要返回的 bandiArray - 如果为非 null,则返回此数组中样本data - 包含图像数据的 DataBuffer
返回:指定像素的指定 band 中的样本另请参见:setSamples(int, int, int, int, int, int[], DataBuffer)
setDataElements
public void setDataElements(int x,
int y,
Object obj,
DataBuffer data)
从 TransferType 类型的基本数组设置指定 DataBuffer 中单个像素的数据。对于 ComponentSampleModel,这与数据类型相同,且每个数组元素返回一个样本。

以下代码说明了从 DataBuffer db1(其存储布局由 ComponentSampleModel csm1 描述)到 DataBuffer db2(其存储布局由 ComponentSampleModel csm2 描述)传输一个像素数据的情况。该传输通常比使用 getPixel 和 setPixel 更有效。

ComponentSampleModel csm1, csm2;
DataBufferInt db1, db2;
csm2.setDataElements(x, y, csm1.getDataElements(x, y, null, db1),
db2);

如果 SampleModel 对象具有相同数量的 band,相应 band 具有相同数量的每样本位数,并且 TransferType 相同,则使用 getDataElements 和 setDataElements 在两个 DataBuffer/SampleModel 对之间传输是合理的。

如果 obj 不是 TransferType 类型的基本数组,则返回 ClassCastException。如果坐标不在边界内部,或者 obj 不是特别大,不能保存像素数据,则可能抛出 ArrayIndexOutOfBoundsException。
指定者:类 SampleModel 中的 setDataElements
参数:x, y - 像素位置的坐标obj - 包含像素数据的基本数组data - 包含图像数据的 DataBuffer另请参见:getDataElements(int, int, Object, DataBuffer)
setPixel
public void setPixel(int x,
int y,
int[] iArray,
DataBuffer data)
使用输入样本的 int 数组设置 DataBuffer 中的像素。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
覆盖:类 SampleModel 中的 setPixel
参数:x, y - 像素位置的坐标iArray - int 数组形式的输入样本data - 包含图像数据的 DataBuffer另请参见:getPixel(int, int, int[], DataBuffer)
setPixels
public void set

Pixels(int x,
int y,
int w,
int h,
int[] iArray,
DataBuffer data)
从包含每个数组元素一个样本的 int 数组设置像素矩形区域的所有样本。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
覆盖:类 SampleModel 中的 setPixels
参数:x, y - 左上角像素位置的坐标w - 像素矩形区域的宽度h - 像素矩形区域的高度iArray - int 数组形式的输入样本data - 包含图像数据的 DataBuffer另请参见:getPixels(int, int, int, int, int[], DataBuffer)
setSample
public void setSample(int x,
int y,
int b,
int s,
DataBuffer data)
使用输入的 int 值,为 DataBuffer 中 (x,y) 处的像素设置指定 band 中的样本。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
指定者:类 SampleModel 中的 setSample
参数:x, y - 像素位置的坐标b - 要设置的 bands - int 类型的输入样本data - 包含图像数据的 DataBuffer另请参见:getSample(int, int, int, DataBuffer)
setSample
public void setSample(int x,
int y,
int b,
float s,
DataBuffer data)
使用输入的 float 值,为 DataBuffer 中 (x,y) 处像素设置指定 band 中的样本。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
覆盖:类 SampleModel 中的 setSample
参数:x, y - 像素位置的坐标b - 要设置的 bands - float 类型的输入样本data - 包含图像数据的 DataBuffer另请参见:getSample(int, int, int, DataBuffer)
setSample
public void setSample(int x,
int y,
int b,
double s,
DataBuffer data)
使用输入的 double 值,为 DataBuffer 中 (x,y) 处像素设置指定 band 中的样本。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。
覆盖:类 SampleModel 中的 setSample
参数:x, y - 像素位置的坐标b - 要设置的 bands - double 类型的输入样本data - 包含图像数据的 DataBuffer另请参见:getSample(int, int, int, DataBuffer)
setSamples
public void setSamples(int x,
int y,
int w,
int h,
int b,
int[] iArray,
DataBuffer data)
根据为每个数据数组元素包含一个样本的 int 数组,设置指定像素矩形的指定 band 中的样本。如果坐标不在边界内部,则可能抛出 ArrayIndexOutOfBoundsException。

覆盖:类 SampleModel 中的 setSamples
参数:x, y - 左上角像素位置的坐标w - 像素矩形区域的宽度h - 像素矩形区域的高度b - 要设置的 bandiArray - int 数组形式的输入样本data - 包含图像数据的 DataBuffer另请参见:getSamples(int, int, int, int, int, int[], DataBuffer)
equals
public boolean equals(Object o)
从类 Object 复制的描述
指示某个其他对象是否与此对象“相等”。

equals 方法在非空对象引用上实现相等关系:

自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。
对称性:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。
传递性:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。
一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。
对于任何非空引用值 x,x.equals(null) 都应返回 false。


Object 类的 equals 方法实现对象上差别可能性最大的相等关系;即,对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true(x == y 具有值 true)。

注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。
覆盖:类 Object 中的 equals
参数:o - 要与之比较的引用对象。
返回:如果此对象与 obj 参数相同,则返回 true;否则返回 false。另请参见:Object.hashCode(),
Hashtable
hashCode
public int hashCode()
从类 Object 复制的描述
返回该对象的哈希码值。支持该方法是为哈希表提供一些优点,例如,java.util.Hashtable 提供的哈希表。

hashCode 的常规协定是:

在 Java 应用程序执行期间,在同一对象上多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。
如果根据 equals(Object) 方法,两个对象是相等的,那么在两个对象中的每个对象上调用 hashCode 方法都必须生成相同的整数结果。
以下情况不 是必需的:如果根据 Object.equals(https://www.doczj.com/doc/fc15907065.html,ng.Object) 方法,两个对象不相等,那么在两个对象中的任一对象上调用 hashCode 方法必定会生成不同的整数结果。但是,程序员应该知道,为不相等的对象生成不同整数结果可以提高哈希表的性能。


实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。(这一般是通过将该对象的内部地址转换成一个整数来实现

的,但是 JavaTM 编程语言不需要这种实现技巧。)
覆盖:类 Object 中的 hashCode
返回:此对象的一个哈希码值。另请参见:Object.equals(https://www.doczj.com/doc/fc15907065.html,ng.Object),
Hashtable

概述 
软件包 
 类 
使用 
树 
已过时 
索引 
帮助 

JavaTM 2 PlatformStandard Ed. 5.0
 上一个类 
 下一个类
框架  
 无框架  
 
!--
if(window==top) {
document.writeln('所有类');
}
//--
所有类
摘要: 嵌套 | 字段 | 构造方法 | 方法
详细信息: 字段 | 构造方法 | 方法
提交错误或意见有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。 版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。


相关主题
文本预览
相关文档 最新文档