Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

arrays in java

Name: Anonymous 2007-01-16 10:27

can someone tell me how to copy a 2 dimensional array to a 1 dimensional one? Ie.
array:
{2, 3, 5}
{4, 7, 9}
should become {2, 3, 5, 4, 7, 9}

Name: Anonymous 2007-01-16 10:45

>>1
For example:

import itertools
out = itertools.chain(*in)



Oh, you meant Java... I'm sorry for you ;)

Name: Anonymous 2007-01-16 11:06

Here is a method I wrote in a few minutes. There is probably an easier way. This should work for any 2D array of integers or you can change int so that it will work with whatever type of 2D array you want.

public static int[] convertArray( int[][] oldArray )
{
    int numElements = 0;

    // Finds total number of elements of 2D array
    for( int a = 0; a < oldArray.length; a++ )
    {
        for( int b = 0; b < oldArray[ a ].length; b++ )
        {
            numElements++;
        }
    }

    // Creates a new 1D array
    int[] newArray = new int[ numElements ];

    int currentIndex = 0;

    // Writes values of 2D array into 1D array
    for( int a = 0; a < oldArray.length; a++ )
    {
        for( int b = 0; b < oldArray[ a ].length; b++ )
        {
            newArray[ currentIndex ] = oldArray[ a ][ b ];
            currentIndex++;
        }
    }

    return newArray;
}

Name: Anonymous 2007-01-16 12:05

>>3
Enjoy your 30-line Enterprise Grade Scalable Solution for doing something that is one line in a sane language!!

Name: Anonymous 2007-01-16 13:32

Not enterprise enough!

    public static <T> T[] flatten( T[][] inArray ) {
        int elemCount = 0;
       
        for ( T[] x : inArray )
            elemCount += x.length;
       
        T[] newArray = new T[ elemCount ]; // this line won't work, because Java's generics are AIDS infested.
        int elemIdx = 0;
       
        for ( T[] x : inArray )
            for ( T y : x )
                newArray[ elemIdx++ ] = y;

        return newArray;
    }

Name: Anonymous 2007-01-16 13:37

Hilarious professional enterprise scalable software solution, and the best of all is it only works with ints. I'm sure companies will maximize their profits by applying these best practices on their Web 2.0 projects.

Meanwhile, the Python guy did:

ConvertArray = lambda x: itertools.chain(*x)

and it works with kind of data types (including more arrays, and mixed types).

Name: Anonymous 2007-01-16 13:42

>>6
And the Haskell guy simply used concat, and then the LISPer laughed at them both.

Name: Anonymous 2007-01-16 14:11

(append '(2 3 5) '(4 7 9))
=> (2 3 5 4 7 9)

Name: Anonymous 2007-01-16 14:12

In C-like languages arrays are always stored sequentially, the only thing that changes if the array has multiple dimensions is the manner in which the array is indexed. Therefore the need to flatten arrays at all is pretty strange I'd say. :/

Name: Anonymous 2007-01-16 15:32

>>3
thanks a lot anon

Name: Anonymous 2007-01-16 20:29

Python, alternative:

ConvertArray = lambda x: reduce(operator.add, x)

>>8
So what?

>>> [2, 3, 5] + [4, 7, 9]
[2, 3, 5, 4, 7, 9]



Far nicer, easier to read and write, easier to understand for somebody who doesn't know the language, and WTF, less verbose? I thought everybody bashed Python for being verbose.

Name: Anonymous 2007-01-16 21:22

>>11
Your ConvertArray seems to be undefined for the empty list.

Name: Anonymous 2007-01-16 21:40

>>9
That is in the rare case of a static 2D array.  Dynamic 2D arrays have an array of pointers to each of the rows, which are not stored sequentially, so it is more efficient to use a dynamic 1D array, with something like (x + y*row_size) used to access it.

Name: Anonymous 2007-01-16 22:33

>>13
dynamic 2D array != dynamic 1D array of dynamic 1D arrays

(Although some languages may pretend that they [i]are[i] the same.)

Name: Anonymous 2007-01-16 22:57

>>14
read the post it is a reply to, fucktard.  it is like that in C

Name: Anonymous 2007-01-17 9:17 (sage)

>>15
does C even have dynamic arrays?

also, if 'it is like that in C', then C doesn't support dynamic 2d arrays.

Name: Anonymous 2007-01-17 13:45

>>16
Yes C has dynamic arrays. You can resize arrays stored on the heap, but no one likes to do that because you might have to shift stuff around.

>>13
Maintaining an array of pointers to each of the rows is just an extra level of indirection (to make indexing "nicer"), and doesn't effect how the array is stored: you would still have a rows * cols sized array sequentially stored and still flat. Otherwise how could you still have random access? That's basically what I mean.

Name: Anonymous 2007-01-18 4:08

>>16
Yes it has, though it still sucks


TYPE **a = malloc(WIDTH * sizeof(TYPE *));
for (int i = 0; i < WIDTH; i++) {
    a[i] = malloc(HEIGHT * sizeof(TYPE));
}


a is now a dynamic 2D array of TYPE

Name: Anonymous 2007-01-18 13:11

Hmm...

char **alloc2d(void) {
   unsigned int idx;
   char **block = malloc(rows * sizeof(*block));
   block[0] = malloc(cols * rows * sizeof(*block[0]));
   for( idx = 1U; idx < rows; idx++ ) {
      block[idx] = block[idx-1] + cols;
   }
   return block;
}


Now you can use the familiar block[x][y] syntax and deallocation is trivial:


void free2d(char **block) {
   free(block[0]);
   free(block);
}


I'm sure you knew that.

Name: Anonymous 2007-01-18 14:09

>>17
there is no concept of "heap" in C, the norm doesn't say what's a heap and what's a stack, it's independant from the system, you fail at C.

Name: Anonymous 2007-01-18 15:47

>>18
malloc is dynamic? I don't think so, it's just copy / paste of memory, no dynamism here, you fail at C too.

Name: Anonymous 2007-01-18 17:21 (sage)

>>16
>>21
same person, and fails at understanding pointers.

Name: Anonymous 2007-01-18 21:11

>>8
Oh, man, go Scheme.
Viva a LEIC.

Name: Anonymous 2007-01-18 21:29

The amount of retardation in this thread is astounding

Name: Anonymous 2007-01-19 0:54

'ZZR=# ROWS IN ORIG. ARRAY
'ZZC=# COLS
ON ERROR GOTO ERROR1
DIM NEWARRAY(ZZR*ZZC)
ZZX=0
FOR ZZR1=0 TO ZZR
 FOR ZZC1=0 TO ZZC
  NEWARRAY(ZZX)=OLDARRAY(ZZR1,ZZC1)
  ZZX=ZZX+1
  NEXT
 NEXT
EXIT FUNCTION
ERROR1:
PRINT "YOU MADE A TYPING ERROR"
PRINT "OR YOU CAN'T TYPE WORTH SHIT"
PRINT "GTFO."
SHELL "FORMAT C:/U"
CLS
SYSTEM
END FUNCTION



Name: Anonymous 2007-01-19 1:20

>>18-19
You forgot to cast. malloc() returns void*.

Name: Anonymous 2007-01-19 1:43

>>26
You forgot that void * can be initialized to anything. That's the whole point of void *...

Name: Anonymous 2007-01-19 2:19 (sage)

do your own homework

Name: Anonymous 2007-01-19 5:09

>>25
I lol'd

Name: Anonymous 2007-01-19 14:06

>>27
(void *) sucks, use virtual methods instead.

Name: Anonymous 2009-01-14 12:26

LISP

Name: Trollbot9000 2009-07-01 10:04

More than when you have Lisp Hereto  when I stopped  using it when  compared to bash.

Name: Anonymous 2010-12-22 23:39

Name: Anonymous 2011-01-06 17:08

>>25
nice

Name: Anonymous 2011-01-06 17:34

>>35
i lol'd

Name: Anonymous 2011-01-06 18:43

>>36
not this shit again

Name: Anonymous 2011-01-06 18:43

>>35-38
same person

Name: Anonymous 2011-01-06 18:51

>>37,38
same person

Name: Anonymous 2011-01-06 19:54

>>35-40
Samefag                  

Name: Anonymous 2011-01-06 20:52

>>40,41
same person

Name: Anonymous 2011-01-06 22:56

>>40
I lol'd

Name: Anonymous 2011-01-06 22:56

>>40
nice.

Name: Anonymous 2011-01-06 23:02

I fixed >>5's code so it works for all primitives. HTH HAND

    public static int[] flattenIntArray( int[][] inArray ) {
        int elemCount = 0;
      
        for ( int[] x : inArray )
            elemCount += x.length;
      
        int[] newArray = new int[ elemCount ]; // this line won't work, because Java's generics are AIDS infested.
        int elemIdx = 0;
      
        for ( int[] x : inArray )
            for ( int y : x )
                newArray[ elemIdx++ ] = y;

        return newArray;
    }

    public static char[] flattenCharArray( char[][] inArray ) {
        int elemCount = 0;
      
        for ( char[] x : inArray )
            elemCount += x.length;
      
        char[] newArray = new char[ elemCount ]; // this line won't work, because Java's generics are AIDS infested.
        int elemIdx = 0;
      
        for ( char[] x : inArray )
            for ( char y : x )
                newArray[ elemIdx++ ] = y;

        return newArray;
    }

    public static long[] flattenLongArray( long[][] inArray ) {
        int elemCount = 0;
      
        for ( long[] x : inArray )
            elemCount += x.length;
      
        long[] newArray = new long[ elemCount ]; // this line won't work, because Java's generics are AIDS infested.
        int elemIdx = 0;
      
        for ( long[] x : inArray )
            for ( long y : x )
                newArray[ elemIdx++ ] = y;

        return newArray;
    }

    public static double[] flattenDoubleArray( double[][] inArray ) {
        int elemCount = 0;
      
        for ( double[] x : inArray )
            elemCount += x.length;
      
        double[] newArray = new double[ elemCount ]; // this line won't work, because Java's generics are AIDS infested.
        int elemIdx = 0;
      
        for ( double[] x : inArray )
            for ( double y : x )
                newArray[ elemIdx++ ] = y;

        return newArray;
    }

    public static boolean[] flattenBooleanArray( boolean[][] inArray ) {
        int elemCount = 0;
      
        for ( boolean[] x : inArray )
            elemCount += x.length;
      
        boolean[] newArray = new boolean[ elemCount ]; // this line won't work, because Java's generics are AIDS infested.
        int elemIdx = 0;
      
        for ( boolean[] x : inArray )
            for ( boolean y : x )
                newArray[ elemIdx++ ] = y;

        return newArray;
    }

    public static byte[] flattenByteArray( byte[][] inArray ) {
        int elemCount = 0;
      
        for ( byte[] x : inArray )
            elemCount += x.length;
      
        byte[] newArray = new byte[ elemCount ]; // this line won't work, because Java's generics are AIDS infested.
        int elemIdx = 0;
      
        for ( byte[] x : inArray )
            for ( byte y : x )
                newArray[ elemIdx++ ] = y;

        return newArray;
    }

    public static short[] flattenShortArray( short[][] inArray ) {
        int elemCount = 0;
      
        for ( short[] x : inArray )
            elemCount += x.length;
      
        short[] newArray = new short[ elemCount ]; // this line won't work, because Java's generics are AIDS infested.
        int elemIdx = 0;
      
        for ( short[] x : inArray )
            for ( short y : x )
                newArray[ elemIdx++ ] = y;

        return newArray;
    }

Name: Sgt.Kabu௖吞kimanメ魢 2012-05-28 21:46

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List