Why is this ArrayList empty when I print it out?
ArrayList<InterviewQuestion> interviewQuestionArrayList = new ArrayList<>();
for (int i = 0; i <interviewQuestionArrayList.size(); i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
System.out.println(interviewQuestionArrayList);
You have created arraylist but before adding you are trying to looping through it.. size will be zero initially..
If you know how many interviewQuestions are there then you can loop with that number.
ex:
for (int i = 0; i <numberOfQuestions; i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
numberOfQuestions should be predifined..
Initially if you dont know how many times you want to add then you can use while loop. But you should exit from while based on some condition
like,
boolean flag = true;
while(flag) {
interviewQuestionArrayList.add(new InterviewQuestion());
if(condition) flag = false;
} //something like this
As far as i can see, your List is empty when you enter the loop.
Thus its size() will return 0 and the loop will not add any elements.
your code basically does
for (int i = 0; i < 0; i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
Here you create an empty list -> this means the size is currently 0
ArrayList<InterviewQuestion> interviewQuestionArrayList = new ArrayList<>();
The following loop is never executed because i (now 0) is never < size (now 0)
This means there are no Objects added to your list
for (int i = 0; i <interviewQuestionArrayList.size(); i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
Try using i<10 as the condition in the for loop to add 10 elements to the list
if this 2 staments are followed by each other, then your list is never populated:
ArrayList<InterviewQuestion> interviewQuestionArrayList = new ArrayList<>();
for (int i = 0; i <interviewQuestionArrayList.size(); i++) {
interviewQuestionArrayList.add(new InterviewQuestion());
}
ergo, you have an empty list to print
System.out.println(interviewQuestionArrayList);
you can play with some dummyData that you add when declaring the list
Related
After some research, I found out how to find the index of an item within a 2D Array. However, I'm after just one value, the row number and also what if the item you are looking for appeared more than once?
How would you store the row number of all those times?
for(int j = 0; j < size; j++)
{
if (arr[i][j] == 88)
{
return i; // The value i wanna store
break;
}
}
If the number 88 appears more than once, how can I store all the different locations and later retrieve it?
You could store the values you want in a List.
List<Integer> rows = new ArrayList<>();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (arr[i][j] == 88) {
rows.Add(i); // The value i wanna store
break; // exit inner loop and continue with next row
}
}
}
i'm after just one value, the row number
But if 88 appears more than once, how can I store all the different
locations and later retrieve it?
Considering you don't know how many duplicated copies of the value you're looking for there could be, I'd suggest using an ArrayList to store the indexes.
create this before the loops:
List<Integer> indexList = new ArrayList<>();
then within the if block simply add the index value for the value you've found to the ArrayList:
if (arr[i][j] == 88){
indexList.add(i);
break;
}
you can then return the ArrayList if your method requires returning the data:
return indexList; // after the loops have finished processing
However, if the method return type is void then you can simply ignore the return indexList;
I've got something like this:
List<int[]> myList= new ArrayList<int[]>();
int tmp[] = new int[size];
for(int i = 0; i<anotherList.size(); i++) {
for (int j = 0; j<size; j++) {
tmp[j] = anotherList.get(i)[j];
}
myList.add(tmp);
}
When I want to print myList, I see only last added tmp. Why?
for(int i = 0 ; i<myList.size(); i++){
for(int j=0; j<myList.get(i).length; j++){
System.out.print(myList.get(i)[j]+ " ");
}
System.out.println();
}
Your code only ever creates one array named "tmp". You need to move the declaration of tmp inside the first for loop, instead of having it before the for loop.
The tmp array is constructed outside the loops, and thus the same instance is being added to myList. To fix, add a different instance to the List at every iteration (eg create the array inside the first for loop)
public boolean isTwoPair() {
boolean isTwoPair = false;
Collections.sort(deck);
List<Card> cards = new LinkedList<Card>(deck);
System.out.println(cards);
for (int i = 0; i < cards.size()-1; i++) {
for (int j = i + 1; j < cards.size()-1; j++) {
if (deck.get(i).equals(deck.get(j))) {
cards.remove(i);
cards.remove(j);
System.out.println(cards);
}
}
}
return isTwoPair;
}
I think my problem is with my cards.remove(). When I remove a card, the next time the card is removed it removes it from an altered list. Is there a way to remove two items from a list while they have the same index numbers?
If I have to remove index 0,1 because they are both pairs like so:
[Ace,Ace,Three,Four,Four]
the code removes it like this(removing index 0)
[Ace,Three,Four,Four]
than instead of removing the index 1(Ace) from the first list it removes it from the second list so
[Ace,Four,Four]
It removed the index 1 from the second list which was three.
This is what I was expecting
[Three,Four,Four]
At this point I expect my loop to pick up and remove Four and Four
EDIT:
public boolean isTwoPair() {
boolean isTwoPair = false;
Collections.sort(deck);
List<Card> cards = new LinkedList<Card>(deck);
System.out.println(cards);
for (int cardOne = 0; cardOne < cards.size(); cardOne++) {
for (int cardTwo = cardOne + 1; cardTwo < cards.size(); cardTwo++) {
if (deck.get(cardOne).equals(deck.get(cardTwo))) {
cards.remove(cardOne);
cards.remove(cardTwo-1);
System.out.println(cards);
for(int cardThree = 0; cardThree < cards.size(); cardThree++){
for(int cardFour = cardThree+1; cardFour < cards.size(); cardFour++){
if(cards.get(cardThree).equals(cards.get(cardFour))){
cards.remove(cardThree);
cards.remove(cardFour-1);
System.out.println(cards);
isTwoPair = true;
}
}
}
}
}
}
return isTwoPair;
}
this is what I am using now, I didn't really want to make a new variable if I didn't have to so I decided to not remove Object
If you know j is always greater than i (because of the for loop), if you delete the element with index i first then you can delete the element with index j - 1 getting the expected result.
cards.remove(i);
cards.remove(j-1);
Use List.remove(Object) instead of List.remove(int). That will not depend on the ordering of the List anymore.
public boolean isTwoPair() {
boolean isTwoPair = false;
Collections.sort(deck);
List<Card> cards = new LinkedList<Card>(deck);
for (int i = 0; i < cards.size()-1; i++) {
for (int j = i + 1; j < cards.size()-1; j++) {
cardI = deck.get(i);
cardJ = deck.get(j);
if (cardI.equals(cardJ)) {
cards.remove(cardI);
cards.remove(cardJ);
isTwoPair = true;
}
}
}
return isTwoPair;
}
The problem is that when you remove an object from the list you are not accounting for the fact that the size is not the same after removal and that indices of elements are changed after removal
Suppose you want to remove elements at and 0 and 1
initially => [Ace,Ace,Three,Four,Four]
0 1 2 3 4
remove [0]
after removing [0] => [Ace,Three,Four,Four]
0 1 2 3
remove [1]
after removing [1] => [Ace,Four,Four]
0 1 2
Thus you cant us indices for removal, instead use List.remove(Object)
cards.remove(i);
cards.remove(j);
As soon as you remove an element from the LinkedList, it shifts any subsequent elements to the left (subtracts one from their indices). You could use j-1 instead of j if j > i. Or, cards.remove(Card) instead.
I have an arraylist populated by four elements, the order of which is random (they are put here by random from another arraylist). I then have a for loop that repeats 10 times, at the end of each repetition I use the clear methods to clear all the elements of the arraylist. However, when I start a new repetition, I would like to repopulate my arraylist with the old (previously worked with) elements that were members of the list in the previous repetition, so that I can use the elements again. And I would like to repeat that until I get out of my 10-repetition for loop. Is there any way to achieve this at all?
Code in addition to my question:
ArrayList<String> answerPegs = new ArrayList<String>();
// add element to ArrayList
ArrayList<String> mySecretAnswer = new ArrayList<String>();
for (int n = 4; n > 0; n--)
{
//populate mySecretAnswer with elements from answerPegs
}
ArrayList<String> clone1 = mySecretAnswer;
for (int q = 0; q < 10; q++) {
for (o = 0; o < 4; o++)
{
}
// called clear() method here
} // END OF 10-ROW LOOP
I would suggest simply having 2 lists - keep a pristine copy of the original list, and then iterate over + clear a copy of that list.
public void doRepetitions(List<Object> original)
{
for( int i=0; i<10; i++ )
{
List<Object> working = new ArrayList<Object>( original );
doStuffWithList(working);
}
}
Edit:
Since you've posted your code, I can give a more specific answer:
You can change your clone to be:
ArrayList<String> clone1 = new ArrayList<String>(mySecretAnswer);
And then move that to be inside your for loop:
for (int q = 0; q < 10; q++)
{
ArrayList<String> clone1 = new ArrayList<String>(mySecretAnswer);
// ....
}
Could you use 2 loops nested and just have the inner loop be the for loop 10 times then clear once at the end
for (int i=1;i<10;i++)
{
if something == true
do something
else
do something
}
Here is what I want to do:
Outside the loop For, I need to summarize that at which i something = true and at which i something = false.
List<Integer> positiveResults = new ArrayList<Integer>();
List<Integer> negativeResults = new ArrayList<Integer>();
for (int i = 1; i < 10; i++)
{
if (someCondition)
positiveResults.add(i);
else
negativeResults.add(i);
}
where someCondition is supposed to be a boolean variable or expression.
If you explicitly want the results in an array instead of a List, then add
Integer[] resultsInArray = positiveResults.toArray(
new Integer[positiveResults.size()]);
You seem to have your for loop set up wrong, the conditional statement should be in the middle:
for (int i = 1; i < 10; i++)
but to answer the question, you just need to declare the variable before the loop:
int i = 1;
for ( ; i < 10; i++) {
}
// You can still access i here since it is still in scope.
First, your loop conditions are in the wrong order for what you probably want:
for (int i = 1; i < 10; ++i)
Next, I guess you want
if (something == true)
{
// do something
}
else
{
// do something *else*
}
in the loop body.
Anyway, in the do something part, you have the value of i since it's locally scoped. At that point you can add it to an array of your choosing.
Example with List:
List<Integer> trueStates = new ArrayList<Integer>();
for (int i=1;i<10;i++)
{
if (something == true) //e.g (i%2 == 0)
trueStates.add(i);
}
After for-loop you will have in trueState i which something == true. Another option is use Map which hold you state of all yours numbers.
Example with Map:
Map<Integer, Boolean> statesMap = new HashMap<Integer, Boolean>();
for (int i=1;i<10;i++)
{
statesMap.put(i, something == true);
}