# Class StringArray

java.lang.Object
org.snpsift.annotate.mem.arrays.StringArrayBase
org.snpsift.annotate.mem.arrays.StringArray

All Implemented Interfaces:
  `Serializable, Iterable<String>`

public class StringArray extends StringArrayBase
The StringArray class implements a memory-efficient array of strings. It stores strings as bytes in a byte array, using UTF-8 encoding, and uses a single byte to mark the end of a string ('\0'). This class provides methods to calculate the size of strings in memory, create StringArray instances from arrays or collections of strings, and retrieve or set strings at specific indices. The internal data structure uses a byte array to store the string data and an integer array to map array indices to offsets in the byte array. Limitations: - The class does not support dynamic resizing of the internal byte array, so the initial size must be sufficient to store all strings. - Once a string is set at a specific index, it cannot be changed or removed. - The class is not thread-safe and should be used with caution in concurrent environments. - Adding strings beyond the allocated memory will result in a runtime exception.

See Also:
  - Serialized Form

## Field Summary

Fields
Modifier and Type
Field
Description
`protected byte[]`
`data`
`protected int[]`
`index2offset`
`static final int`
`MAX_NUM_STRING_TO_SHOW`
`protected int`
`offset`

### Fields inherited from class StringArrayBase

`currentIndex`

## Constructor Summary

Constructors
Constructor
Description
`StringArray(int numElements, int size)`
Constructor

## Method Summary

Modifier and Type
Method
Description
`String`
`get(int i)`
Get the string at array index 'i'
`int`
`getOffset()`
`static StringArray`
`of(String[] strings)`
Create a StringArray from an array of strings
`static StringArray`
`of(Collection<String> strings)`
Create a StringArray from a collection of strings
`int`
`set(int i, String str)`
Add a string to the array WARNING: Typically you use either 'add' or 'set', but not both
`int`
`size()`
Size of the data array (capacity of number of bytes)
`long`
`sizeBytes()`
Memory size of this object (approximate size in bytes)
`static int`
`sizeOf(String s)`
Calculate the size of a string in memory
`static int`
`sizeOf(String[] strings)`
`static int`
`sizeOf(Collection<String> strings)`
`String`
`toString()`

### Methods inherited from class StringArrayBase

`add, iterator, length`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface Iterable

`forEach, spliterator`

## Field Details

### MAX_NUM_STRING_TO_SHOW

public static final int MAX_NUM_STRING_TO_SHOW

See Also:
  - Constant Field Values

### data

protected byte[] data

### index2offset

protected int[] index2offset

### offset

protected int offset

## Constructor Details

### StringArray

public StringArray(int numElements, int size)
Constructor

Parameters:
  `numElements` - : Number of elements in the array
  `size` - : Initial size of the array

## Method Details

### sizeOf

public static int sizeOf(String s)
Calculate the size of a string in memory

### sizeOf

public static int sizeOf(String[] strings)

### sizeOf

public static int sizeOf(Collection<String> strings)

### of

public static StringArray of(String[] strings)
Create a StringArray from an array of strings

### of

public static StringArray of(Collection<String> strings)
Create a StringArray from a collection of strings

### get

public String get(int i)
Get the string at array index 'i'

Specified by:
  `get` in class `StringArrayBase`

### getOffset

public int getOffset()

### set

public int set(int i, String str)
Add a string to the array WARNING: Typically you use either 'add' or 'set', but not both

Specified by:
  `set` in class `StringArrayBase`

### size

public int size()
Size of the data array (capacity of number of bytes)

Specified by:
  `size` in class `StringArrayBase`

### sizeBytes

public long sizeBytes()
Memory size of this object (approximate size in bytes)

Specified by:
  `sizeBytes` in class `StringArrayBase`

### toString

public String toString()

Overrides:
  `toString` in class `Object`