-4

I'm working on a Java software project and I want to create a "waterfall" display, in which I need to draw a row of color-coded data at the bottom of the screen, and then "move" the rows up as the data ages and new rows are created. It seems to me that although it's possible to create the row data and then move the row-arrays through a 2d array representing the display area, that this isn't a very efficient way to go about things.

Obviously, if I were writing this in C or C++ I'd use pointers (and arrays of pointers).

How do I implement this in Java?

Johan
  • 74,508
  • 24
  • 191
  • 319
Rich
  • 4,157
  • 5
  • 33
  • 45
  • 4
    This looks more like a rant on Java language than a proper question. – Luiggi Mendoza Mar 20 '13 at 15:32
  • Everything that is an Object is basically passed around as a pointer in Java. So everything not a 'primitive' is what I mean. – Sanchit Mar 20 '13 at 15:34
  • 1
    You could use a LinkedList that conveniently supports O(1) deletion and insertion anywhere in the list, instead of an array that requires shifting items around. That said, exactly how much data are we talking about? 80 rows of characters? Updated maybe once a second? A array will likely be fast enough, seeing how the Android device is capable of rendering 30fps 3D animations. – millimoose Mar 20 '13 at 15:35
  • In Java, references are reseatable. If you want to think in C++ you should think of them as pointers that don't allow pointer arithmetic rather than as C++ references. – JoeG Mar 20 '13 at 15:35
  • The fact that you're so focused on why the C++ solution you have in mind won't translate also means this question manifests the XY problem - you spend half of it talking about a proposed nonsolution that you can't use anyway, instead of actually clearly outlining your problem, how you've tried to solve it (in Java), and why that doesn't work. – millimoose Mar 20 '13 at 15:39
  • Damn, and I thought apps on my Android had fluid scrolling. Where is that C++ phone? – Bruno Grieder Mar 20 '13 at 15:40
  • The arrays in question will be defined by the size of my Nexus 7 display (minus screen widgets), so about 500 rows of about 1200 pixels. Given that this is really a screen graphics problem, are there existing Android screen graphics tools I might consider instead? – Rich Mar 20 '13 at 16:43

2 Answers2

3

In Java arrays are not real bidimensinal arrays in any case. They are arrays of arrays. So you can easily do it through references:

public void foo() {
  int[][] data = new int[32][32];
  ...
  for (int i = 32; i > 0; --i)
    data[i-1] = data[i];
}

This is somewhat similar in how you would do it in C++

public void foo() {
  int **data = new int*[32];
  for (int i = 0; i < 32; ++i)
    data[i] = new int[32];
  ...
  for (int i = 32; i > 0; --i)
    data[i-1] = data[i];
}
Jack
  • 131,802
  • 30
  • 241
  • 343
  • You're missing one of the subscripts in your references to "data". Or I am missing something you implied, but didn't spell out explicitly. Should there be another variable in there?? – Rich Mar 20 '13 at 16:45
  • 1
    It should if you are trying to access a specific element. Aren't we talking about moving __one entire row__ of an array? That's how it is done. By moving the reference to the specific row in the bidimensional array. – Jack Mar 20 '13 at 17:42
  • Then that would be the obvious thing you implied, but I didn't get. Sorry for my ignorance, but your solution sounds way easier than I expected. I shall definitely have to give it a try. – Rich Mar 20 '13 at 17:49
1

Java has a LinkedList class that works just like a (doubly) linked list in C, only in an Object Oriented(TM) way.

See: http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html
and: When to use LinkedList over ArrayList?

Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319
  • There seems to be a great deal of assumption about why I asked the original question. Really, I'm asking because I'm trying to solve a real problem in a realistic way. I get that I can't use pointers -- I'm not whining about reality. Thanks for providing a workable approach, and I'll see what I can do with it. – Rich Mar 20 '13 at 17:34
  • In Java pointers have been replaced with Objects. Just think of Objects as (pointers to Objects) and note that there are no pointers to non-Objects. – Johan Mar 20 '13 at 17:38
  • And some people don't know how to read. As far as I was concerned the question was clear: how do I do X? – Johan Mar 20 '13 at 17:39