Print out all elements of a stack/queue [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to go through a stack and a queue and print out the Object's values. In my Object's class I have implemented a toString. My stack and queue classes use a linked list. I tried going through it with a for loop like an array first, obviously doesn't work. I know how to get the top but not all of them.
for (i = 0; i <= 9; i++) {
System.out.println(storageA[i].toString());
}
storageA is my stack with fixed size of 10.

Assuming you want to iterate over the complete list holding Objects of type E:
for (E element : storageA) {
System.out.println(element.toString());
}
If you really only want elements 0-9 (better validate storageA.size > 9, else you get an IndexOutOfBoundsException):
for (i = 0; i <= 9; i++) {
System.out.println(storageA.get(i).toString());
}

Related

How come for Integer arrays, as in Integer arr[] = new Integer[], you can't change their value, you can only set them? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I was doing a problem in Codeforces and had to use an Integer arr[] = new Integer[200001]. I wanted to store 200001 counters for their index values(for example, if I got 19 then arr[19] would be the number of 19s that appeared in the array). However, when I went to do arr[num]++ or arr[num] += 1, it would not work. Only when you set arr[num] to some number does it work. Is there a way to get around that?
It wouldn't work because you haven't initialized each element of the array, you just have an Integer array with null references.
you should first do:
for(int i = 0; i < arr.length; i++) {
arr[i] = 0;
}
The easiest way is to use an int array instead of an Integer array.

How can I check if an ArrayList contains a specific number of instances of a certain element? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So I'm programming a Go Fish game for a project of mine, and I've been at this for a while. Currently I'm trying to check if the player/computer has a full book of a certain number. I've based the whole game off of arrays, and just need to check if the array contains four instances of a given element.
I'm not sure about the specifics but this should get you started.
int count = 0;
for(int i = 0; i < YOURARRAY.length; i++)
{
if( //check to see if the element is the type youre counting)
{
count++;
}
}
if(count == 4)
{
//there are 4 instances
}

how to find max 2 element in 10 element less complexity in java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
can you please tell me how to find max 2 element in 10 element less complexity in java .
I do like this but it complexity is too high .i need to reduce that
Algo
take max =a[0]
for(int i =0;i<10;i++){
if(a[i]>max)
max=a[i]
}
Second way to sort the array using bubble sort then find the last 2 element ?
Instead of sorting the array, you can simply do the following:
Keep a largestValue and a secondLargestValue
Loop through the entire array once, for each element:
Check to see if the current element is greater than largestValue:
If so, assign largestValue to secondLargestValue, then assign the current element to largestValue (think of it as shifting everything down by 1)
If not, check to see if the current element is greater than secondLargestValue
If so, assign the current element to secondLargestValue
If not, do nothing.
O(n) run time
O(1) space requirement
max =a[0];
max2 = a[0];
for(int i =0;i<10;i++)
{
if(a[i]>max)
{
max2=max;
max=a[i];
continue;
}
if(a[i]>max2||max2==max)
{
max2=a[i];
}
}

Best algorithm to remove elements between 2 given indices [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I'm trying to learn algorithm efficiency. I know how to remove one element from the array, but not sure how to remove between two indices. Lets pretend that the
list = {1,2,3,4,5,6,7,8,9,10}, and we call the removeBetween method with arguments:
removeBetween(2, 6);
public void removeBetween(int FirstIndex, int LastIndex)
{
}
A general algorithmic direction you can follow :
To remove all numbers between 2 given indices, say (FirstIndex, LastIndex):
Copy all elements from index 0 to FirstIndex to a result array.
Next copy all elements from LastIndex to Array.lenght()-1 indices to the same result array above.
Return result.
so if you have your items in an array list you can do something like:
ArrayList<Whatever object is> newElements = yourlist.sublist( 0, firstIndex );
newElements.addAll( yourlist.subList( LastIndex + 1, yourlist.size() ) ;

How to check the positive and negative number's in a given arraylist in android or java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have set of int Arraylist for my application.For example
int[] array={10,8,-4,-6,5}
Here i want to check this array list.i am using this array for my bar graph chart.if all the value contains positive value,i want to show the graph in 0 to 100 % Y axis.if array contains negative value,i want to show the negative axis like -50 to 100 % depends upon the value.
How to do the logic for this problem.can anyone help me for my application?
boolean isNegative=false;
for(int i=0;i<array.length;i++)
{
if(array[i]<0)
{
isNegative=true;
break;
}
}
if(isNegative)
{
// code for displaying negative axis
}
else
{
//only display positive axis
}

Categories

Resources