# Character ArrayList (with small Editor for one line of text)

**URL:** <https://discourse.processing.org/t/character-arraylist-with-small-editor-for-one-line-of-text/40967>\
**Category:** Beginners\
**Created:** [February 20, 2023, 8:27pm UTC](https://discourse.processing.org/t/character-arraylist-with-small-editor-for-one-line-of-text/40967 "2023-02-20T20:27:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanto2016](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@nanto2016](https://discourse.processing.org/u/nanto2016)\
**Post date:** [February 20, 2023, 8:27pm UTC](https://discourse.processing.org/t/character-arraylist-with-small-editor-for-one-line-of-text/40967/1 "2023-02-20T20:27:30Z")

</div>

I want to create an ArrayList of chars, how would I do this?  
`ArrayList<char> a = new ArrayList<char>();` doesn’t work. Same for all primitive types, but `int` and some others have their own list datatypes. so I could use an `IntList` (since `char` is super easily convertible to `int`), but in my situation I would need to convert from a String to IntList, how do I do that?

If it is not possible to either create a char ArrayList or an IntList converted from a String, I would need to be able to remove elements from a `char[]`, as I would use it instead of a List. And I’m pretty confused on how to remove elements from arrays, too…

---

<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:** [February 20, 2023, 8:44pm UTC](https://discourse.processing.org/t/character-arraylist-with-small-editor-for-one-line-of-text/40967/2 "2023-02-20T20:44:44Z")

</div>

> [@nanto2016](#):
>
> `ArrayList<char> a = new ArrayList<char>();`

`final ArrayList<Character> chars = new ArrayList<Character>();`  
[docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Character.html](http://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Character.html)

> [@nanto2016](#):
>
> , but in my situation I would need to convert from a String to IntList. How do I do that?

```auto
final IntList letters = new IntList(int(JAVA2D.toCharArray()));
{
  println(letters);
  println(str(char(letters.values())));
}

```

[docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#toCharArray()](http://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#toCharArray())

> [@nanto2016](#):
>
> , I would need to be able to remove elements from a `char[]`,

```auto
final StringBuilder sequence = new StringBuilder(OPENGL);
{
  println(sequence);
}

```

[docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/StringBuilder.html](http://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/StringBuilder.html)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 20, 2023, 9:52pm UTC](https://discourse.processing.org/t/character-arraylist-with-small-editor-for-one-line-of-text/40967/3 "2023-02-20T21:52:07Z")

</div>

A good solution has been shown with arraylist

A list of char is also a String.

There are powerful commands, like charAt, indexOf … substring…

**Example**

Here is an example that shows how you can split a String at a cursor pos “a”

```auto
int a = 4; // cursor pos

String myText="ThisIsATextThatYouCanType";

void setup() {
  size(600, 600);
  background(0);
}

void draw() {
  background(0);
  text(myText+ " " + "Use Cursor left / right ", 12, 55);
  text(myText.substring(0, a), 22, 77);
  text(myText.substring(a), 54, 99);
}

void keyPressed() {
  // For CODED --------------------------------------------
  if (key==CODED) {

    switch(keyCode) {
    case UP:

      break;
    case DOWN:

      break;
    case LEFT:
      a--;
      break;
    case RIGHT:
      a++;
      break;
    }//switch
    return; // leave
  }// if CODED
  //
}

```

**New Version with more Editor Functions**

- blinking cursor
- Backspace
- Delete
- enter text

```auto
int a = 4;

String myText="ThisIsATextThatYouCanType";

boolean blinkFlag;

void setup() {
  size(600, 600);
  background(0);
}

void draw() {
  background(0);
  text(myText+ " " + "Use Cursor left / right; Backspace, Delete and just type text ", 12, 55);
  text(myText.substring(0, a), 22, 77);
  text(myText.substring(a), 54, 99);

  text(myText.substring(0, a)
    + blink()
    + myText.substring(a), 22, 192);
}

void keyPressed() {
  // For CODED --------------------------------------------
  if (key==CODED) {
    //
    switch(keyCode) {
    case UP:
      break;
    case DOWN:
      break;
    case LEFT:
      a--;
      break;
    case RIGHT:
      a++;
      break;
    }//switch
    return; // leave
  }// if CODED
  //
  //
  // not CODED: -----------------------------------
  if (key==BACKSPACE) {
    myText = myText.substring(0, a-1) +
      myText.substring(a);
    a--;
  } else if (key==DELETE) {
    myText = myText.substring(0, a) +
      myText.substring(a+1);
  } else if (key==ESC) {
    key=0; // kill ESC
  } else if (key>=32) {
    // Enter normal text
    myText = myText.substring(0, a)
      + key
      + myText.substring(a);
    a++;
  }
}//func

String blink() {
  if (frameCount%19==0)
    blinkFlag=
      ! blinkFlag;

  if (blinkFlag)
    return "|";
  else return " ";
}
//

```

**3rd version**

bit more security checks for start and end of word…

```auto

// Mini Editor

String myText="ThisIsATextThatYouCanType";

int cursorPosition = 4;

boolean blinkFlag;

void setup() {
  size(600, 600);
  background(0);
}

void draw() {
  background(0);

  // Help text
  fill(255, 0, 0);
  text ( "Use Cursor left / right; Backspace, Delete, Return and just type text ", 12+22+189, 22);

  // normal text
  fill(0, 222, 111);
  text(myText, 12, 55);

  // text in two lines
  fill(255);
  text(myText.substring(0, cursorPosition), 22, 99);
  text(myText.substring(cursorPosition), 54, 99+20);

  // Text with cursor
  text(myText.substring(0, cursorPosition)
    + blink()
    + myText.substring(cursorPosition), 22, 192);
}

// ----------------------------------------------------------------------------------------------------

void keyPressed() {
  // For CODED ----------------------
  if (key==CODED) {
    //
    switch(keyCode) {
    case UP:
      break;
    case DOWN:
      break;
    case LEFT:
      cursorPosition--;
      break;
    case RIGHT:
      cursorPosition++;
      break;
    }//switch

    // check cursorPosition
    if (cursorPosition<0)
      cursorPosition=0;
    if (cursorPosition>=myText.length())
      cursorPosition=myText.length();

    return; // leave function
  }// if CODED
  //
  //
  // not CODED: -------------
  if (key==BACKSPACE) {
    if (cursorPosition==0)
      return; // No
    myText = myText.substring(0, cursorPosition-1) +
      myText.substring(cursorPosition);
    cursorPosition--;
  } else if (key==DELETE) {
    if (cursorPosition>=myText.length())
      return;
    myText = myText.substring(0, cursorPosition) +
      myText.substring(cursorPosition+1);
  } else if (key==ESC) {
    key=0; // kill ESC
  } else if (key==ENTER||key==RETURN) {
    println(myText);
  } else if (key>=32) {
    // Enter normal text
    myText = myText.substring(0, cursorPosition)
      + key
      + myText.substring(cursorPosition);
    cursorPosition++;
  }// else if
  //
}//func

String blink() {
  // blinking cursor |
  if (frameCount%19==0)
    blinkFlag=
      ! blinkFlag;

  if (blinkFlag)
    return "|";
  else return " ";
} // func
//

```
