Home Removed Value from arraylist and Assign to this value into a one dimensional array
Reply: 2

Removed Value from arraylist and Assign to this value into a one dimensional array

Saswati Bhattacharjee
1#
Saswati Bhattacharjee Published in 2018-02-13 21:42:27Z
ArrayList<Integer> list2 = new ArrayList<Integer>();
int a[] = new int[list2.size()];
list2.add(5);
list2.add(2);
list2.add(3);

Iterator<Integer> list3 = list2.iterator();

while (list3.hasNext()) {
    if (list2.size() != 0) {
        ArrayList<ArrayList> list1 = new ArrayList<ArrayList>();
        list1.add(list2);
        System.out.println(list1);
        list2.remove(list2.size() - 1);
        System.out.println(list1);
    } else {
        System.out.println("Stop");

    }
}

This is a small code of Java that is implemented by myself as a beginner of Java. I add the element of list2 into list1. Then I remove the element from the list2 so after a certain time list1 is also zero. I also implemented a array a which is of size list2. I want to add those removed element into the array a such as a[0]=3; a[1]=2; a[2]=5. So what code should I write to assign the removed value from list2 into array a.

Nikolas Charalambidis
2#
Nikolas Charalambidis Reply to 2018-02-13 22:48:36Z

I understand your question as you want to remove all the items from the List one-by-one and insert in the array.

Firstly, I see that you declare and initialize a new List with each loop that is wrong. Secondly, you set the array size before there is know the final size of the List you take the items from.

Consider the following code and follor the output of the filling array:

List<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(2);
list.add(3);
int array[] = new int[list.size()];
int index = 0;

Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
    array[index++] = iterator.next();
    iterator.remove();
    System.out.println(Arrays.toString(array));
}

To use array is not a good idea for storing values continuosly since it's final size has to be defined before the usage. The better way is to use another list which could be converted to the array since you insist to an array output.

List<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(2);
list.add(3);

List<Integer> temp = new ArrayList<Integer>();
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
    temp.add(iterator.next());
    iterator.remove();
    System.out.println(temp);
}

int[] array = temp.stream().mapToInt( (Integer i) -> i.intValue()).toArray();
System.out.println(Arrays.toString(array));

Finally I am a bit confused how useful this approach would be. As exercise, fine.

Take That 101
3#
Take That 101 Reply to 2018-02-13 22:26:21Z

Here are the answers to your two questions:

First: to int. The simple code here does that.

int[] a = list2.stream().mapToInt(i -> i).toArray();

Second: You wanted the array reversed. to accomplish this, I dug up this code.

        for (int start = 0, end = a.length - 1; start <= end; start++, end--) {
            int aux = a[start];
            a[start]=a[end];
            a[end]=aux;
        }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323137 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO