1

I have a methode in2.getImagesOneDim() which gives me an array of integers, to be more precise the pixel values of an image. Now i want to create one big array with all the pixel values of all the images. Therefore I have to call this method several times. Now I would like to concatenate the previous output to the current output until all images are read.

In some kind of pseudo code, where the + is a concatination ... :

 for (int i = 1; i < 25; i++) {
  ConArray = ConArray + in2.getImagesOneDim("../images/"+i);
 }

How would I do this in java ?

3
  • It is just pseudo code. I see it as the total array with all the pixel values Commented Nov 18, 2012 at 10:48
  • You already know the size is gonna be 24, why not just use an array? Commented Nov 18, 2012 at 10:49
  • because the in2.getImagesOneDim method does not return 1 value, it returns an array with a variable length of pixels. Therefore it is not possible to get the exact size of the eventual array Commented Nov 18, 2012 at 10:52

3 Answers 3

1

Split big problem into many smaller and You find the way:

  1. Read each image into separate array
  2. Compute total size
  3. Join sub arrays

See example

import java.util.Arrays;

public class ImageProgram {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new ImageReader().readImages()));
    }
}

class ImageReader {

    public int[] readImages() {
        int[][] images = readImagesInto2DArray();
        return makeOneDimensionArray(images);
    }

    private int[] makeOneDimensionArray(int[][] arrays) {
        int totalLength = computeTotalLength(arrays);
        int[] array = new int[totalLength];
        int destPos = 0;
        for (int[] subArray : arrays) {
            System.arraycopy(subArray, 0, array, destPos, subArray.length);
            destPos += subArray.length;
        }
        return array;
    }

    private int computeTotalLength(int[][] arrays) {
        int length = 0;
        for (int[] subArray : arrays) {
            length += subArray.length;
        }
        return length;
    }

    private int[][] readImagesInto2DArray() {
        int size = 24;
        int[][] result = new int[size][];
        for (int i = 0; i < size; i++) {
            result[i] = readArray(i + 1);
        }
        return result;
    }

    private int[] readArray(int index) {
        // read from file
        int[] result = new int[index];
        Arrays.fill(result, index);
        return result;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

where would my method come into place ? (in2.getImagesOneDim("../images/"+i)), which returns an array of int
Use Your method instead of my "readArray" method. You could put "in2" object as a argument to method "readImages".
0
List<int[]> pixels = new ArrayList<int[]>();
for (int i = 1; i < 25; i++) {
    pixels.add(in2.getImagesOneDim("../images/"+i));
}
return pixels;

Now you have a list of all the data, you can get the data with pixels.get(index);

5 Comments

I need an array, not an arraylist, this gives me an array list of arrays ..., how would the conversion to an array work?
@Ojtwist Sorry, I don't think you can convert it in an array. But you can just use pixels.get(5) instead of pixels[5]. Works the same.
That is not an option in my framework, it is supposed to work on the GPU with JOCL and it does not allow arraylists
@Ojtwist I am not familiar with JOCL, but can't you send them one by one?
That would defy the purpose of running it on a GPU or generally parallel. I need one big array of values :)
0

You could use the Collections#addAll method.

List<Integer> allIntegers = new ArrayList<Integer>();

for (int i = 1; i < 25; i++) {
    Integer[] imageIntegers = in2.getImagesOneDim("../images/"+i);
    allIntegers.addAll(Arrays.asList(imageIntegers));
}

return allIntegers.toArray();

3 Comments

Is it possible to work with int instead of Integer ? Because my framework does not allow for Integer just int.
Well you can convert them if you have access to the apache lang package, commons.apache.org/lang/api-release/org/apache/commons/lang3/…
When You convert array to list, and then when You concatenate lists, and after all, when You convert list to array - JVM invokes a lot of "copy" and "resize" methods. To avoid this You should directly work on raw array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.