# Creating dynamic array

**URL:** <https://discourse.processing.org/t/creating-dynamic-array/31794>\
**Category:** Coding Questions\
**Created:** [August 19, 2021, 7:48pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794 "2021-08-19T19:48:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [August 19, 2021, 7:48pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/1 "2021-08-19T19:48:31Z")

</div>

I am missing some commands, so if you can help me - that would be awesome.

I want to creat a dynamic array, that stores the keypressed, and how many times each one pressed

for example, let’s say the user pressed the key L

if the user pressed the L key for the first time, I want add L,0 to the array  
if the user pressed the L key for the third time, I want to change the objects from L,2 to L,3

which commands do I need?

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [August 19, 2021, 8:29pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/2 "2021-08-19T20:29:39Z")

</div>

Unfortunately, in Java it’s not so intuitive. Instead of a regular array, you can use `ArrayList`.

> **[Reference](https://processing.org/reference/ArrayList.html)**
>
> An ArrayList stores a variable number of objects. This is similar to making an array of objects, but with an ArrayList, items can be easily added and removed from the ArrayList and it is…

If you have specific questions, it would be great if you can post your code to show where you are stuck 🙂

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 19, 2021, 8:35pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/3 "2021-08-19T20:35:03Z")

</div>

You can use an IntList container for that:  
`final IntList keyPressCount = new IntList();`

> **[Reference](https://processing.org/reference/IntList.html)**
>
> Helper class for a list of ints. Lists are designed to have some of the features of ArrayLists, but to maintain the simplicity and efficiency of working with arrays. Functions like sort()

Use its method **increment()** to increase the count of desired index which represents your pressed key:  
`keyPressCount.increment('L'); // increase count to index L`  
`keyPressCount.increment(keyCode); // increase count to last pressed key`

> **[Reference](https://processing.org/reference/IntList_increment_.html)**
>
> Add one to a value.

Call method **get()** in order to read current count for a specific key index:  
`println(keyPressCount.get('L')); // read count for index L`  
`println(keyPressCount.get(keyCode)); // read count for last key pressed`

> **[Reference](https://processing.org/reference/IntList_get_.html)**
>
> Get an entry at a particular index.

Here’s a sample sketch using IntList and its methods **increment()** & **get()**:

```auto
/**
 * IntList KeyCode Count (v1.0.1)
 * GoToLoop (2021/Aug/19)
 * https://Discourse.Processing.org/t/creating-dynamic-array/31794/3
 */

final IntList keyPressCount = new IntList();

void setup() {
  size(400, 300);
  noLoop();

  keyPressCount.increment('L');
  println(keyPressCount.get('L')); // 1

  keyPressCount.increment('L');
  keyPressCount.increment('L');
  println(keyPressCount.get('L')); // 3
}

void draw() {
  background((color) random(#000000));
}

void keyPressed() {
  final int k = keyCode;

  keyPressCount.increment(k);
  println((char) k, k, keyPressCount.get(k));

  redraw();
}

```

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [August 19, 2021, 8:42pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/4 "2021-08-19T20:42:24Z")

</div>

You can also use a HashMap. HashMaps can store things in pairs, like the key being pressed and the count. Here is the syntax:

```auto
HashMap<String, Integer> letters = new HashMap<String, Integer>();

```

You can loop through the HashMap to check whether the key being pressed matches one of the keys in the HashMap, and if it does, use the replace() method in the HashMap to alter the count of the number of times the key was pressed. You can check this by looping through the keySet in the HashMap by using the keySet() method. If the key pressed doesn’t match any of the items in the HashMap, then you can use the put() method to add a new value and count.

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [August 19, 2021, 8:50pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/5 "2021-08-19T20:50:59Z")

</div>

As a matter of fact, the final target is to analyze pixels of an image, and count how many pixels of each color exist in this image

I still build it in my head…

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [August 19, 2021, 8:53pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/6 "2021-08-19T20:53:55Z")

</div>

ohh, thank you. I never meant to ask someone to write the whole code. It was just an example.

The final purpose of my question is to analazye a JPG image and cound how many pixels of each color exists.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 19, 2021, 9:03pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/7 "2021-08-19T21:03:18Z")

</div>

Then you should check it out my answers from this very old forum: 👓  
[forum.Processing.org/two/discussion/14393/getting-the-dominant-color-of-an-image.html](http://forum.Processing.org/two/discussion/14393/getting-the-dominant-color-of-an-image.html)

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [August 19, 2021, 10:57pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/8 "2021-08-19T22:57:51Z")

</div>

I found it. Thanks.

80% of the code I don’t understand but I got it.

next step is to check for similar colors, means - if you find a color that is similar to another exist color (for example 100 distance) - refer it as it already existed (As I work on photograph and… Unique colors found: 141642 from: 663000

)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 20, 2021, 1:42am UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/9 "2021-08-20T01:42:54Z")

</div>

> [@yalaniv](#):
>
> Unique colors found: 141642 from: 663000

A `color` pixel in Processing is a 32-bit integer (24-bit if we disregard the alpha component):

> **[color / Reference](https://processing.org/reference/color_datatype.html)**
>
> Datatype for storing color values. Colors may be assigned with get() and color() or they may be specified directly using hexadecimal notation such as #FFCC00 or 0xFFFFCC00.…

Maybe you should consider converting from 24-bit to just 8-bit.  
This way you’d have 256 colors instead of 16,777,216!  
Red & green components would be converted from 8-bit (255 max) to a 3-bit (7 max) value.  
While the blue component would be converted from 8-bit (255 max) to a 2-bit (3 max) value.

Check it out the solutions from the link below:

> **[\[Solved\] How to convert a 24bit rgb to 8 bit rgb - CodeProject](https://www.codeproject.com/Questions/1077234/How-to-convert-a-bit-rgb-to-bit-rgb)**
>
> Free source code and tutorials for Software developers and Architects.; Updated: 22 Jun 2019

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [August 20, 2021, 6:04pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/10 "2021-08-20T18:04:59Z")

</div>

No, I don’t want to do that.

I want to use less colors, but I don’t want the only to use limited colors.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [August 22, 2021, 1:03am UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/11 "2021-08-22T01:03:48Z")

</div>

There are many tools and libraries focused on counting groups of colors in an image. In general, counting many colors as one color is called “color quantization”:

> **[Color quantization](https://en.wikipedia.org/wiki/Color_quantization)**
>
> In computer graphics, color quantization or color image quantization is quantization applied to color spaces; it is a process that reduces the number of distinct colors used in an image, usually with the intention that the new image should be as visually similar as possible to the original image. Computer algorithms to perform color quantization on bitmaps have been studied since the 1970s. Color quantization is critical for displaying images with many colors on devices that can only display a li...

Related past discussion:

> [@How to apply color quantization to an image](https://discourse.processing.org/t/how-to-apply-color-quantization-to-an-image/4849/21):
>
> This is an interesting problem and I used to use it years ago to keep image file sizes down but I can’t remember what software I used. So it piqued my interest enough to create a sketch that uses the Quantize class code in the link provided by @jb4x (thanks). So here is the result ouput from my sketch [sample] I have simply created a new tab called Quantize.java and copied the code from the link into it. NOTE that the tab name must match precisely the class name to make it a top level class. …

If you want to group colors using a strategy that maintains visual qualities of the original color space, perhaps start with Median Cut – we had a [past conversation](https://discourse.processing.org/t/how-to-apply-color-quantization-to-an-image/4849/22) pointing to the Java implementation of Median Cut available from ImageJ. javax also has a [ColorQuantizer](https://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/ColorQuantizerDescriptor.html).

OpenCV also has some quantization built-in via k-means clustering. There is a good discussion of approaches using OpenCV here:

> <https://stackoverflow.com/questions/5906693/how-to-reduce-the-number-of-colors-in-an-image-with-opencv>

I’m guessing that is what you mean – rather than using a stock palette, you want to adaptively choose a small number of colors that best approximate the appearance of your original image. Is that right?

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [August 22, 2021, 4:42pm UTC](https://discourse.processing.org/t/creating-dynamic-array/31794/12 "2021-08-22T16:42:22Z")

</div>

Thanks for your reply.

I want to reduce the number of the colors, in order to change it style.

if I need to reduce from 110K to 50K, or any change below 80K will spoil the image? I don’t know yet.  
Maybe I can reduce it to 30K or even to 12K and it still look similar to the original ? I don’t know yet

Will have to run some tests, to find the right number
