0

How do I initialise an arrayList for fixed size and assign it to nil?
I have code as below in Swift/IoS.
What is the equivalent in Java for below code in Java?

var studentsList:[Result?] = [Result?](repeating: nil, count: 5) //assign to nil
studentsList[2] = newStudent //assign at different level Eg:2
studentsList = studentsList.filter { $0 != nil } //filter all the nil objects

I need an array for fixed length.
I am able to add the elements in the order, but I need the ability to add the element at any of the initialised levels.
For example, if I fix the size of array as 5, I should be able to assign object to arrList[2] even though arrList[0] and arrList[1] are still nil

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
dacscan3669
  • 651
  • 2
  • 8
  • 16
  • In English, what are you trying to do here, and more importantly -- why? – DontKnowMuchBut Getting Better Jun 21 '17 at 01:21
  • What do you mean "assign it to nil"? You want to fill the array with a certain number of `null`s? – Carcigenicate Jun 21 '17 at 01:21
  • 1
    I think you mean you want an array with a fixed size not an ArrayList –  Jun 21 '17 at 01:22
  • @DontKnowMuchButGettingBetter nil is null in swift – Cup of Java Jun 21 '17 at 01:22
  • @CupofJava: I figured as much, but the whole thing still smells of being an XY Problem, which is why the *why* is so important. – DontKnowMuchBut Getting Better Jun 21 '17 at 01:24
  • I think this pretty much a dupe of https://stackoverflow.com/questions/5600668/how-can-i-initialize-an-arraylist-with-all-zeroes-in-java but I'm hesitant to call it since it's not clear what the question is. – Carcigenicate Jun 21 '17 at 01:24
  • Possible duplicate of [How can I initialize an ArrayList with all zeroes in Java?](https://stackoverflow.com/questions/5600668/how-can-i-initialize-an-arraylist-with-all-zeroes-in-java) – Isaac Jun 21 '17 at 01:25
  • I need an array for fixed length. I am able to add the elements in the order but what i need is ability to add the element at any of the initialised levels. For example, if I fix the size of array as 5, I should be able to assign object to arrList[2] even though arrList[0] and arrList[1] are still nil. The code i have posted is exactly how it works in Swift/iOS – dacscan3669 Jun 21 '17 at 01:25
  • And you can do that... `arrList[2] = new Student()` – OneCricketeer Jun 21 '17 at 01:26
  • I tried below and it gives me java.util.ArrayList.throwIndexOutOfBoundsException. StudentList.add(1, newStudent); – dacscan3669 Jun 21 '17 at 01:28
  • @dacscan3669 - You're probably accessing an array element outside the declared index –  Jun 21 '17 at 01:28
  • Below is the current code which throws OutofBoundsException. private ArrayList StudentList = new ArrayList<>(5); StudentList.add(1, newStudent); – dacscan3669 Jun 21 '17 at 01:29

5 Answers5

1
ArrayList<Student> studentList = new ArrayList<Student>(Collections.nCopies(5, null));
studentList.set(2, newStudent)
//studentList = studentList.stream().filter(e -> e != null)

Since it seems you're using Android, and streams won't work, use the following:

ArrayList<Student> tempList = new ArrayList<>();
for(Student s : studentList) 
    if(student !=null) 
       studentList.add(student)
Yonah Karp
  • 581
  • 7
  • 22
  • The last command shows an error. cannot resolve method 'stream' hovering on stream and also 'Lambda expressions are not supported at this language level' for (e -> e != null) – dacscan3669 Jun 21 '17 at 02:08
  • @dacscan3669 I assume you're programming for Android then. Streams were added in Java 8, so they aren't available in all android versions – Yonah Karp Jun 21 '17 at 02:09
  • 2
    @dacscan3669 Instead, you'll have to use a loop `for(Student s : studentList) if(student !=null) tempList.add(student)` – Yonah Karp Jun 21 '17 at 02:12
  • @dacscan3669 If my answer helped you out, I'd appreciate if you could accept it (by pressing the green check above). Happy Coding :) – Yonah Karp Jun 21 '17 at 02:48
1

Give what you said in the comments, you're probably looking for:

StudentList[] studentarray = new StudentList[5];
studentarray[2]= studentObj;
1

Whenever you make an array of an Object class, it is all null.

Student[] students = new Student[5];

And you can assign an index, which keeps the rest as null.

students[2] = new Student();

An array is fixed size and cannot be added to.

If you want a stream out of an array, you can do that as well

Arrays.stream(students).filter( Objects::nonNull )
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • The last command shows an error. cannot resolve method 'stream' hovering on stream and also 'Method references are not supported at this language level' for Objects::nonNull – dacscan3669 Jun 21 '17 at 02:01
  • The last command does not work on Android and hence had to go for alternate solution. The first 2 commands are fine. – dacscan3669 Jun 21 '17 at 02:24
  • Java 8 features are being added. https://developer.android.com/guide/platform/j8-jack.html Your question was not tagged with android. – OneCricketeer Jun 21 '17 at 02:26
  • You could also use RxJava, which is an interesting library to wrap Android code with – OneCricketeer Jun 21 '17 at 02:30
0

Try this.

List<Student> students = Arrays.asList(new Student[fixedSize]);

This is a List, not an ArrayList. But you can modify each element. If you add an element to students, exception will be thrown.

0

I resolved by using the below code based on suggestions posted.

Student[] studentList = new SchoolItem[5];

ArrayList<Student> sortedList = new ArrayList<>();

compareList[2] = new Student();

for( Student s : compareList) {
       if(s !=null)
       sortedList.add(s); }
dacscan3669
  • 651
  • 2
  • 8
  • 16