getting the missing numbers in array list
I need to get the missing numbers of an array list, array list gets its numbers from firebase database, so I need to get the missing numbers that are not in the array.
Numbers are in between 1-10 and arrayList contains[3,9,7,5] so it will print "1 2 4 6 8 10" to a list view.
my code it just duplicates the numbers from 1-10 four times for each number. so how can I do this in android.
... here is my code
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ArrayList<String> array = new ArrayList<>();
ArrayList<Integer> arrayList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
students = ds.getValue(Students.class);
studentSeatNum = students.getSeatnum();
arrayList.add(Integer.valueOf(studentSeatNum));// save numbers from fire-base database into array list
}
// get missing values of the array list
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < arrayList.size(); j++) {
if (i != arrayList.indexOf(j)) {
array.add(String.valueOf(i));
}
}
}
adapter = new ArrayAdapter<String>(EmptySeats.this, android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
}
java

|
show 6 more comments
I need to get the missing numbers of an array list, array list gets its numbers from firebase database, so I need to get the missing numbers that are not in the array.
Numbers are in between 1-10 and arrayList contains[3,9,7,5] so it will print "1 2 4 6 8 10" to a list view.
my code it just duplicates the numbers from 1-10 four times for each number. so how can I do this in android.
... here is my code
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ArrayList<String> array = new ArrayList<>();
ArrayList<Integer> arrayList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
students = ds.getValue(Students.class);
studentSeatNum = students.getSeatnum();
arrayList.add(Integer.valueOf(studentSeatNum));// save numbers from fire-base database into array list
}
// get missing values of the array list
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < arrayList.size(); j++) {
if (i != arrayList.indexOf(j)) {
array.add(String.valueOf(i));
}
}
}
adapter = new ArrayAdapter<String>(EmptySeats.this, android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
}
java

What's the problem? Does your code not work?
– Carcigenicate
Jan 1 at 15:59
it duplicate the numbers from 1-10 four times @Carcigenicate
– Sawsan
Jan 1 at 16:02
1
Missing in what way? You're also missing 100, 12345 andInteger.MAX_VALUE
, to name a few of the 4 billion-odd missing values.
– Andy Turner
Jan 1 at 16:07
@AndyTurner , the numbers are in between 1-10 , array list contains [3,9,7,5], print the missing numbers that are not in array
– Sawsan
Jan 1 at 16:16
@ReemH.Alzoubi please edit your question to include relevant details.
– Andy Turner
Jan 1 at 16:19
|
show 6 more comments
I need to get the missing numbers of an array list, array list gets its numbers from firebase database, so I need to get the missing numbers that are not in the array.
Numbers are in between 1-10 and arrayList contains[3,9,7,5] so it will print "1 2 4 6 8 10" to a list view.
my code it just duplicates the numbers from 1-10 four times for each number. so how can I do this in android.
... here is my code
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ArrayList<String> array = new ArrayList<>();
ArrayList<Integer> arrayList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
students = ds.getValue(Students.class);
studentSeatNum = students.getSeatnum();
arrayList.add(Integer.valueOf(studentSeatNum));// save numbers from fire-base database into array list
}
// get missing values of the array list
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < arrayList.size(); j++) {
if (i != arrayList.indexOf(j)) {
array.add(String.valueOf(i));
}
}
}
adapter = new ArrayAdapter<String>(EmptySeats.this, android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
}
java

I need to get the missing numbers of an array list, array list gets its numbers from firebase database, so I need to get the missing numbers that are not in the array.
Numbers are in between 1-10 and arrayList contains[3,9,7,5] so it will print "1 2 4 6 8 10" to a list view.
my code it just duplicates the numbers from 1-10 four times for each number. so how can I do this in android.
... here is my code
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ArrayList<String> array = new ArrayList<>();
ArrayList<Integer> arrayList = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
students = ds.getValue(Students.class);
studentSeatNum = students.getSeatnum();
arrayList.add(Integer.valueOf(studentSeatNum));// save numbers from fire-base database into array list
}
// get missing values of the array list
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < arrayList.size(); j++) {
if (i != arrayList.indexOf(j)) {
array.add(String.valueOf(i));
}
}
}
adapter = new ArrayAdapter<String>(EmptySeats.this, android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
}
java

java

edited Jan 1 at 18:03


Fantômas
32.8k156390
32.8k156390
asked Jan 1 at 15:58
SawsanSawsan
125
125
What's the problem? Does your code not work?
– Carcigenicate
Jan 1 at 15:59
it duplicate the numbers from 1-10 four times @Carcigenicate
– Sawsan
Jan 1 at 16:02
1
Missing in what way? You're also missing 100, 12345 andInteger.MAX_VALUE
, to name a few of the 4 billion-odd missing values.
– Andy Turner
Jan 1 at 16:07
@AndyTurner , the numbers are in between 1-10 , array list contains [3,9,7,5], print the missing numbers that are not in array
– Sawsan
Jan 1 at 16:16
@ReemH.Alzoubi please edit your question to include relevant details.
– Andy Turner
Jan 1 at 16:19
|
show 6 more comments
What's the problem? Does your code not work?
– Carcigenicate
Jan 1 at 15:59
it duplicate the numbers from 1-10 four times @Carcigenicate
– Sawsan
Jan 1 at 16:02
1
Missing in what way? You're also missing 100, 12345 andInteger.MAX_VALUE
, to name a few of the 4 billion-odd missing values.
– Andy Turner
Jan 1 at 16:07
@AndyTurner , the numbers are in between 1-10 , array list contains [3,9,7,5], print the missing numbers that are not in array
– Sawsan
Jan 1 at 16:16
@ReemH.Alzoubi please edit your question to include relevant details.
– Andy Turner
Jan 1 at 16:19
What's the problem? Does your code not work?
– Carcigenicate
Jan 1 at 15:59
What's the problem? Does your code not work?
– Carcigenicate
Jan 1 at 15:59
it duplicate the numbers from 1-10 four times @Carcigenicate
– Sawsan
Jan 1 at 16:02
it duplicate the numbers from 1-10 four times @Carcigenicate
– Sawsan
Jan 1 at 16:02
1
1
Missing in what way? You're also missing 100, 12345 and
Integer.MAX_VALUE
, to name a few of the 4 billion-odd missing values.– Andy Turner
Jan 1 at 16:07
Missing in what way? You're also missing 100, 12345 and
Integer.MAX_VALUE
, to name a few of the 4 billion-odd missing values.– Andy Turner
Jan 1 at 16:07
@AndyTurner , the numbers are in between 1-10 , array list contains [3,9,7,5], print the missing numbers that are not in array
– Sawsan
Jan 1 at 16:16
@AndyTurner , the numbers are in between 1-10 , array list contains [3,9,7,5], print the missing numbers that are not in array
– Sawsan
Jan 1 at 16:16
@ReemH.Alzoubi please edit your question to include relevant details.
– Andy Turner
Jan 1 at 16:19
@ReemH.Alzoubi please edit your question to include relevant details.
– Andy Turner
Jan 1 at 16:19
|
show 6 more comments
1 Answer
1
active
oldest
votes
Solution
If the list will always have numbers between 1-10, for example: [3,7,9]
, use a simple for-loop, like this:
for (int i = 1; i <= 10; i++){
if (!list.contains(i)){
otherlist.add(i);
}
}
Output
the otherlist
will contain [1,2,4,5,6,8,10]
Explanation
the .contains()
method checks if the list already has a certain number, if it does not, you simply add that number (in the examples case, i - the index of the loop), to otherlist
.
you saved my life ... thanks SO much
– Sawsan
Jan 1 at 16:45
@ReemH.Alzoubi you are very welcome :)
– Daniel B.
Jan 1 at 16:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996869%2fgetting-the-missing-numbers-in-array-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Solution
If the list will always have numbers between 1-10, for example: [3,7,9]
, use a simple for-loop, like this:
for (int i = 1; i <= 10; i++){
if (!list.contains(i)){
otherlist.add(i);
}
}
Output
the otherlist
will contain [1,2,4,5,6,8,10]
Explanation
the .contains()
method checks if the list already has a certain number, if it does not, you simply add that number (in the examples case, i - the index of the loop), to otherlist
.
you saved my life ... thanks SO much
– Sawsan
Jan 1 at 16:45
@ReemH.Alzoubi you are very welcome :)
– Daniel B.
Jan 1 at 16:46
add a comment |
Solution
If the list will always have numbers between 1-10, for example: [3,7,9]
, use a simple for-loop, like this:
for (int i = 1; i <= 10; i++){
if (!list.contains(i)){
otherlist.add(i);
}
}
Output
the otherlist
will contain [1,2,4,5,6,8,10]
Explanation
the .contains()
method checks if the list already has a certain number, if it does not, you simply add that number (in the examples case, i - the index of the loop), to otherlist
.
you saved my life ... thanks SO much
– Sawsan
Jan 1 at 16:45
@ReemH.Alzoubi you are very welcome :)
– Daniel B.
Jan 1 at 16:46
add a comment |
Solution
If the list will always have numbers between 1-10, for example: [3,7,9]
, use a simple for-loop, like this:
for (int i = 1; i <= 10; i++){
if (!list.contains(i)){
otherlist.add(i);
}
}
Output
the otherlist
will contain [1,2,4,5,6,8,10]
Explanation
the .contains()
method checks if the list already has a certain number, if it does not, you simply add that number (in the examples case, i - the index of the loop), to otherlist
.
Solution
If the list will always have numbers between 1-10, for example: [3,7,9]
, use a simple for-loop, like this:
for (int i = 1; i <= 10; i++){
if (!list.contains(i)){
otherlist.add(i);
}
}
Output
the otherlist
will contain [1,2,4,5,6,8,10]
Explanation
the .contains()
method checks if the list already has a certain number, if it does not, you simply add that number (in the examples case, i - the index of the loop), to otherlist
.
edited Jan 1 at 16:46
answered Jan 1 at 16:42


Daniel B.Daniel B.
1,3011112
1,3011112
you saved my life ... thanks SO much
– Sawsan
Jan 1 at 16:45
@ReemH.Alzoubi you are very welcome :)
– Daniel B.
Jan 1 at 16:46
add a comment |
you saved my life ... thanks SO much
– Sawsan
Jan 1 at 16:45
@ReemH.Alzoubi you are very welcome :)
– Daniel B.
Jan 1 at 16:46
you saved my life ... thanks SO much
– Sawsan
Jan 1 at 16:45
you saved my life ... thanks SO much
– Sawsan
Jan 1 at 16:45
@ReemH.Alzoubi you are very welcome :)
– Daniel B.
Jan 1 at 16:46
@ReemH.Alzoubi you are very welcome :)
– Daniel B.
Jan 1 at 16:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996869%2fgetting-the-missing-numbers-in-array-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the problem? Does your code not work?
– Carcigenicate
Jan 1 at 15:59
it duplicate the numbers from 1-10 four times @Carcigenicate
– Sawsan
Jan 1 at 16:02
1
Missing in what way? You're also missing 100, 12345 and
Integer.MAX_VALUE
, to name a few of the 4 billion-odd missing values.– Andy Turner
Jan 1 at 16:07
@AndyTurner , the numbers are in between 1-10 , array list contains [3,9,7,5], print the missing numbers that are not in array
– Sawsan
Jan 1 at 16:16
@ReemH.Alzoubi please edit your question to include relevant details.
– Andy Turner
Jan 1 at 16:19