0

Here I have integer array contains 81 values so that I need to store 9 values per array total I need to get 9 arrays. Eg: array1 from 1 to 9 and array2 from 10 to 18 and array3 from 19 to 27 like that. Can anybody help me how to get this values?

public class demo {
   public static void main(String[] args) {
       int num = 81;
       int numArray = num / 9;
        int[] input = new int[num];
        for (int i = 0; i < num; i++) {
            input[i] = i + 1;
        }
        for (int j = 0; j < input.length; j++) {
            System.out.println(input[j]);
        }
    }
}

How to get desired result?

Maroun
  • 94,125
  • 30
  • 188
  • 241
khanam
  • 335
  • 1
  • 4
  • 19
  • what are you trying to display?? and you are not even using the numArray so why is it created , Your question is not very clear –  Dec 10 '14 at 13:16
  • Use either System.arraycopy or one of the methods of the Arrays class. – Hot Licks Dec 10 '14 at 13:16
  • @Mohit_Bhasi,i need to create 9 arrays ,each array contains 9 values. – khanam Dec 10 '14 at 13:17
  • @khanam thanks for the clarification , surely will provide an answer:) –  Dec 10 '14 at 13:18

3 Answers3

0

You can use System#arraycopy():

int[] first_part = new int[27];
int[] second_part = new int[27];
int[] third_part = new int[27];

System.arraycopy(input, 0, first_part, 0, 27);
System.arraycopy(input, 27, second_part, 0, 27);
...

I've just noticed that you want 9 parts, you can easily put this in a loop and use arraycopy.

Maroun
  • 94,125
  • 30
  • 188
  • 241
0
public static void main(String[] args) {
    int num = 85;
    int limit = 5;
    int index = 0;
    int extra = 0;
    int finalArrayIndex = 0;
    int[] innerArray = null;
    int[] input = new int[num];
    boolean isEnd = false;

    if (num % limit > 0) {
        extra = 1;
    }

    int[][] finalArray = new int[(num / limit) + extra][limit];

    for (int i = 0; i < input.length; i = i + (limit)) {

        innerArray = new int[limit];
        for (int j = 0; j < limit; j++) {

            innerArray[j] = input[index++];
            if (index >= input.length) {
                isEnd = true;
                break;
            }
        }

        finalArray[finalArrayIndex++] = innerArray;

        if (isEnd) {
            break;
        }
    }

    // just for test
    for (int k = 0; k < finalArray.length; k++) {
        for (int l = 0; l < finalArray[k].length; l++) {
            System.out.println("finalArray[" + k + "]" + "[" + l + "] : "
                    + finalArray[k][l]);
        }
    }
}

This is dynamic solution, you can change value of num and limit variables. I hope this will help you :)
Java Fiddle link : http://ideone.com/jI8IOb

Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82
0

My suggestion is use subList

You can follow these steps.

  1. Store all 81 values in a List (ArrayList)
  2. Then create 9 sub List from that using subList()
  3. You can convert List to array.

    List<Integer> fullList=new ArrayList<>();        
    List<Integer> list1=fullList.subList(0,8); // first 9 elements
    Integer[] bar = list1.toArray(new Integer[list1.size()]);
    
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Ruchira Gayan Ranawee,without using any builtin function,is it not possible? – khanam Dec 22 '14 at 12:46
  • @khanam No. you don't need to use build in methods. you can write a code to do it. I suggest this since it is less to do. – Ruchira Gayan Ranaweera Dec 22 '14 at 12:48
  • @Could you please help me to get my desired output. – khanam Dec 22 '14 at 12:49
  • @khanam can you please post what you want to do and so far what you have tried? you can reach me in skype or using a e-mail since you can't use chat in stackOverFlow yet. – Ruchira Gayan Ranaweera Dec 22 '14 at 12:51
  • Ruchira Gayan Ranaweera,check this link http://stackoverflow.com/questions/27602719/how-to-print-non-repeated-numbers-from-integer-array-using-java?noredirect=1#comment43628402_27602719 – khanam Dec 22 '14 at 12:54