How to Create a Java Spreadsheet Data Structure

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Create a Java Spreadsheet Data Structure
Store spreadsheet data with Java. (number background image by kuhar from

At the simplest level, you can create a data structure for a spreadsheet in Java using a simple 2D array. However, this approach leaves a great deal to be desired. Ideally, a spreadsheet data structure should be able to efficiently resize, insert, and delete entire rows and columns, and these operations are computationally expensive in a Java array, requiring Java to rebuild the entire data structure from scratch behind the scenes each time such an operation is performed.

Instead, you should use something a bit more sophisticated: a linked list of linked lists. This will allow the insertion and removal of rows and columns as well as the resizing of the table to happen much more quickly. However, it will come with a small performance cost when it comes time to access specific cells of the spreadsheet out of turn.

Skill level:

Other People Are Reading


  1. 1

    Create a new Java file and name it "" If you have a favourite Integrated Development Environment (IDE), you can do this by clicking "File," "New Class." Alternatively, you can open Windows Notepad by clicking "Start," "All Programs," "Accessories," "Notepad" and saving the file with that file name.

  2. 2

    Create your class interface by pasting the following into the file you created in Step 1. The rest of the steps will be dedicated to filling in the code for each of the methods defined in this step:

    public class Spreadsheet {

    LinkedList<LinkedList> data = new LinkedList<LinkedList>();

    public Spreadsheet() { }

    public void add Row() { }

    public void add Row(int index) { }

    public void remove Row(int index) { }

    public void remove Column(int index) { }

    public void add Column() { }

    public void add Column(int index) { }

    public void set Cell(int x, int y, String new Data) { }

    public int get Width() { }

    public int get Height() { }

    public String to String() { }


    * Test the class,


    public static void main(String[] args) { }


    The basic data structure will be a LinkedList of rows containing a LinkedList of columns. Though a simple 2D array would be simpler to implement, it would also be much slower for many common spreadsheet operations, especially inserting and deleting rows.

  3. 3

    Paste the following code between the brackets of the constructor method, "Spreadsheet":

    public Spreadsheet() {

    add Row();

    add Column();


    This simply ensures that we start with at least a single row and column.

  4. 4

    Paste the following to fill in the two add Row methods. One takes no arguments and automatically adds the row at the end of the spreadsheet, while the other allows the user to specify where to add the row:

    public void add Row() {

    data.addLast(new LinkedList<String>());

    for (int x = 0; x < get Height(); x++) {

    data.getLast().add(new String());



    public void add Row(int index) {

    data.add(index, new LinkedList<String>());

    for (int x = 0; x < get Height(); x++) {

    data.get(index).add(new String());



  5. 5

    Paste the code to remove a row into your source file:

    public void remove Row(int index) {



  6. 6

    Paste the code for the two add Column methods. These function similarly to the add Row methods--there are two, one for adding a column to the end of the sheet and one for inserting a column in the middle:

    public void add Column() {

    for (LinkedList l : data) {

    l.addLast(new String());



    public void add Column(int index) {

    for (LinkedList l : data) {

    l.add(index, new String());



  7. 7

    Paste the code for removing columns:

    public void remove Column(int index) {

    for (LinkedList l : data) {




  8. 8

    Paste the code that sets the contents of a given cell to a string of data:

    public void set Cell(int x, int y, String new Data) {

    data.get(x).set(y, new Data);


  9. 9

    Fill in the methods for getting the width and height of the spreadsheet:

    public int get Width() {

    return data.getFirst().size();


    public int get Height() {

    return data.size();


  10. 10

    Implement the to String method. This will return a formatted string displaying, in a table of rows and columns, the data in the spreadsheet:

    public String to String() {

    String temp = "";

    for (LinkedList l : data) {

    for (Object o : l) {

    String s = (String) o;

    if (s.equals("")) s = "-empty-";

    temp += s + " ";


    temp += "\n";


    return temp;


  11. 11

    Paste the following main method that puts the spreadsheet data structure through its paces and ensures everything works as expected:

    public static void main(String[] args) {

    Spreadsheet sheet = new Spreadsheet();



    System.out.print(" x ");




    System.out.print(" x ");








    sheet.setCell(1,0, "TEMP");

    sheet.setCell(1,1, "TEMP");







Don't Miss


  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.