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

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…

1 Like

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

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()

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

docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/StringBuilder.html

4 Likes

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”

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
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…


// 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
//

2 Likes