Jump to content

Java - Funny things with ArrayList

- - - - -

  • Please log in to reply
No replies to this topic

#1
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Fun thing with java is the ArrayList, gotta love it, "when" it is "useful". I found this really amusing due to I couldn't count how much it would be by adding 1-100...
So made a simple program to solve it for me :)

BTW, I used java since I know it better than C++/C# and python...However I will train more in c++/c# and python when I have time :)

import java.util.ArrayList;

public class ArrayListArray {


  public static void main(String[] args) {


    ArrayList<Integer> al = new ArrayList<Integer>();


   

    for(int j=0; j<=100; j++) {

    	al.add(j);

    

    System.out.println("1-100: "+ al );

    Object ia[] = al.toArray();

    int sum = 0;

    

    for(int i=0;i<ia.length;i++) 

      sum+=((Integer)ia[i]).intValue();

    

    System.out.println("Sum av 1-100 :"+ sum);

    }

  }

}
Hope you guys can use it whenever you feel like it :)
Posted Image




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users