-9

I am having a ArrayList<String> abc in which I am getting this value [64.3, 25.1, 44.3, 34.2, 6.4, 48.5, 35.5, 59.5, 54.6, 26.6, 11.2, 50.3, 25.1]

Now I want to extract the integer values from it such as 64,25,44,34,6,48,35,59,54,26,11,50,25 and put these values in an integer array int[] cab

How can I get this? How to remove the decimal and after decimal values from it?

Andreas
  • 154,647
  • 11
  • 152
  • 247
Android Geek
  • 598
  • 1
  • 5
  • 20
  • but this is ArrayList not double array – Android Geek Feb 05 '16 at 07:21
  • 2
    @AndroidGeek Don't you think that would be a very important piece of information to add to the question? *(click "edit" link)* I mean, it looks like an `ArrayList` to anyone else, so saying that they are `String` objects is kinda important, you know. – Andreas Feb 05 '16 at 07:23
  • use the iterator to extract the value from array list and cast to int – KVK Feb 05 '16 at 07:24
  • @Andreas I have mentioned it in the question topic – Android Geek Feb 05 '16 at 07:32
  • 1
    why people have downvoted my question without reading it carefully. It''s not my mistake it's the readers mistake – Android Geek Feb 05 '16 at 08:05

7 Answers7

3

Here are three different (but similar) ways of doing it, depending on Java version and/or preferences:

ArrayList<String> abc = new ArrayList<>(Arrays.asList(
        "64.3", "25.1", "44.3", "34.2", "6.4", "48.5", "35.5",
        "59.5", "54.6", "26.6", "11.2", "50.3", "25.1" ));
System.out.println("abc = " + abc);

// Loop (all Java versions)
int[] cab1 = new int[abc.size()];
for (int i = 0; i < cab1.length; i++)
    cab1[i] = (int)Double.parseDouble(abc.get(i));
System.out.println("cab1 = " + Arrays.toString(cab1));

// Java 8 stream with lambda expression
int[] cab2 = abc.stream()
                .mapToInt(v -> (int)Double.parseDouble(v))
                .toArray();
System.out.println("cab2 = " + Arrays.toString(cab2));

// Java 8 stream with method references
int[] cab3 = abc.stream()
                .map(Double::valueOf)
                .mapToInt(Double::intValue)
                .toArray();
System.out.println("cab3 = " + Arrays.toString(cab3));

Output

abc = [64.3, 25.1, 44.3, 34.2, 6.4, 48.5, 35.5, 59.5, 54.6, 26.6, 11.2, 50.3, 25.1]
cab1 = [64, 25, 44, 34, 6, 48, 35, 59, 54, 26, 11, 50, 25]
cab2 = [64, 25, 44, 34, 6, 48, 35, 59, 54, 26, 11, 50, 25]
cab3 = [64, 25, 44, 34, 6, 48, 35, 59, 54, 26, 11, 50, 25]
Andreas
  • 154,647
  • 11
  • 152
  • 247
1

Simply iterate:

int[] cab = new int[abc.size()];
for(int i = 0; i < cab.length; i++){
    cab[i] = (int)Float.parseFloat(abc.get(i));
}
Derlin
  • 9,572
  • 2
  • 32
  • 53
1

Try This :

Integer cab[] = new Integer[abc.size()];
cab= abc.toArray(cab);

//iterating array
for (Integer cabs: cab) {
System.out.println("cab= " + cabs);
}
Sakam24
  • 121
  • 5
0
  1. string to double

  2. double to int

    for(int i = 0 ; i < abc.size(); ++i) {
        tmp = Double.parseDouble(abc.get(i));
        cab[i] = (int)tmp;
        System.out.println(cab[i]);
    }
    
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
Steve YonG
  • 80
  • 7
0

In Java 8:

int[] cab = abc.stream()
        .map(Double::valueOf)
        .mapToInt(Double::intValue)
        .toArray();
shmosel
  • 49,289
  • 6
  • 73
  • 138
0
ArrayList<Integer> newList = new ArrayList<Integer>(stringsList.size());
for (String myInt : strings){
    int intVal = (int)Double.parseDouble(myInt);
    newList.add(intVal);
}

where stringsList is your Arraylist

Dhinakaran Thennarasu
  • 3,336
  • 4
  • 26
  • 39
0

use this code

Iterator itr = al.iterator();
          while(itr.hasNext()) {

             String s1=itr.next().toString();
             Double d=new Double(s1);
             int s2=d.intValue();
             System.out.print(s2 + " ");
          }
KVK
  • 1,257
  • 2
  • 17
  • 48
  • Why not use a `for` loop? Why not use generics? `Double.parseDouble(s1)` would be better, since it doesn't create a wrapper object for the result. – Andreas Feb 05 '16 at 16:44