ArrayList<Rolling> copy = new ArrayList<Rolling>();
for (int i = 0; i < sequence.size(); i++) {
copy.add(sequence.get(i));
}
ListIterator<Rolling> listIterator = copy.listIterator();
// Remove each element one by one
for (int j = 0; j < copy.size(); j++) {
listIterator.next();
if (copy.contains()) {
listIterator.remove();
}
}
public static void main(String[] args) {
remove(sequenceOfDice(4), 4);
}
}
short summary: I have a class called Rolling and I wanna thru this method copy its elements and then remove the value of n from the new list and return the rest but I am not geting far since i got a couple of errors.
The error I get is:The method contains(Object) in the type ArrayList is not applicable for the arguments ()
the method "copy.contains()" should have a parameter(argument).
that means,you should write like this "copy.contains(x)"
what's more, i think you are a new learner.because your code have many errors or sames strange.
your comment is "// Remove each element one by one"
but you said "then remove the value of n from the new list", is this conflict?
please paste all the code and compile it successfully on your laptop.
By the way, in a for loop, we usually use iterator to remove element because remove element will cause modcount change,try below code:
while(listIterator.hasNext()){
Rolling next = listIterator.next();
if (copy.contains(next)) {
listIterator.remove();
}
}
Related
I put a partial of my code which I think is the source of problem but I could not figure out hence why I am at StackOverFlow now. Anyways this Class is where i set my data and pass it into an array.
public ArrayList<select.rates> caseGetRates() throws RateTableException, SessionDisconnectedException {
try {
for(int i=0;i < arrayRate.size();i++){
ArrayList<select.rates> arr = new ArrayList<select.rates>();
this.setPair(array[0]);
this.setBid((array[2]));
this.setAsk((array[3]));
arr.add(this);
}
return arr;
} finally{}
}
When I System.out.print the data which I set in this class it gives me:
EUR/USD
1.12372
1.12384
USD/JPY
100.622
100.641
which is correct and what I would like it to be displayed on my webpage.However when I pass the data to my Servlet
try {
ArrayList<select.rates> rates = example.caseGetRates();
for(int i=0;i < rates.size();i++){
System.out.println("");
System.out.println(rates.get(i).getPair());
System.out.println(rates.get(i).getBid());
System.out.println(rates.get(i).getAsk());
}
request.setAttribute("rates", rates);
}
request.getRequestDispatcher("/NewFile.jsp").forward(request, response);
The result I get on my Servlet is:
USD/JPY
100.622
100.641
USD/JPY
100.622
100.641
The result does loop twice however the data seems to be overwritten and I still can't figure out why is this happening. I hope someone can pin point my mistake.
Create ArrayList object outside for loop
and inside for loop create new Object that you are adding to ArrayList
try {
ArrayList<select.rates> rates = example.caseGetRates();
for(int i=0;i < rates.size();i++){
// create new object here and then add to ArrayList
}
request.setAttribute("rates", rates);
}
request.getRequestDispatcher("/NewFile.jsp").forward(request, response);
So I have a method to take a stack of Integers, and return the stack with all of the elements duplicated, and in the same order. My problem is with the method i currently have, Im getting an infinite loop problem on all cases except when the Stack is empty. What can i do to complete the duplication without a looping problem?
public void stutter(Stack<Integer> Integ)
{
Integer first;
for(int i = 0; i < Integ.size(); i++)
{
first = Integ.pop();
Integ.push(first);
Integ.push(first);
}
}
Each time you push another integer, you increase the original size of your stack, pushing your "i" limit forward.
You should return a new Stack, preferably using (pre java8):
public Stack<Integer> stutter(Stack<Integer> from) {
Stack<Integer> stk = new Stack<>();
for(Integer i: from) {
stk.push(i);
stk.push(i);
}
return stk;
}
ofc, its an inifinite loop. You increase the Integ.size() inside of the loop by Integ.push().
Try something like that. Save the size inside a var befor starting to push new elements into it.
int size = Integ.size();
for(int i = 0; i < size; i++){
first = Integ.pop();
Integ.push(first);
Integ.push(first);
}
Just create new stack with duplicates (and replace old if needed).
I am attempting to access an ArrayList that was created in a different method within the same class. The scanner method pulls in data from a text file. The text file data appears this way: 123 12 1 43, with line breaks...
Currently, my method works to pull in the data, but does not compile after that method ends. I originally had the entire code within the same method and it worked fine. But I'd like to return the largest value by creating a new method within this class, and then create a tester class that will access this new method. Here is my existing code. Or if there is a better solution. I'm all ears.
public class DataAnalyzer {
public DataAnalyzer(File data) throws FileNotFoundException
{
List<Integer> rawFileData = new ArrayList<>();
FileReader file = new FileReader("info.txt");
try (Scanner in = new Scanner(file)) {
while(in.hasNext())
{
rawFileData.add(in.nextInt());
}
}
}
public int getLargest(rawFileData){
int largest = rawFileData.get(0);
for (int i = 1; i < rawFileData.size(); i++){
if (rawFileData.get(i) > largest)
{
largest = rawFileData.get(i);
}
}
for (Integer element : rawFileData){
if (element == largest)
{
System.out.print("This is the Largest Value: ");
System.out.print(element);
}
}
}
}
Your main issue is with your method declaration. It needs a type parameter:
public int getLargest(List<Integer> rawFileData)
Note the List<Integer>.
Now, there is already a method for this in the Collections utility class. You would do well to look over that link in detail - there are many useful methods there. To get the highest element from a Collection of Objects that have a natural order (such a Integer). For example
int largest = Collections.max(rawFileData)
So your method can be reduced to:
public int getLargest(List<Integer> rawFileData)
return Collections.max(rawFileData);
}
You need to think over your logic much more carefully before you begin to write code, for example, your first loop is good:
int largest = rawFileData.get(0);
for (int i = 1; i < rawFileData.size(); i++){
if (rawFileData.get(i) > largest)
{
largest = rawFileData.get(i);
}
}
You do exactly what any programmer would do. But then, instead of returning the largest when you find it, you for some reason loop again:
for (Integer element : rawFileData){
if (element == largest)
{
System.out.print("This is the Largest Value: ");
System.out.print(element);
}
}
Ask yourself what does this do? You have a List of, say, apples. You look at each one and compare them - finding the largest apple. You now have the largest apple in the List. You then loop over the List again looking for an apple that matches the apple you have already found. Why do this?
Further, you never return from the method. Your method is declared as returning an int; but you never do.
The missing type in your method definition is the problem here.
Change the method definition from
public int getLargest(rawFileData) {
....
}
to
public void getLargest(List<Integer> rawFileData) {
....
}
And the second for loop in the method is unnecessary. The largest integer is already stored in the variable "largest" and you can print it after the first for loop.
I have a class that called Entries that holds my constructor along with its getters and setters.
In a new class, I have :
private LinkedList<Entry>[] Entries = new LinkedList[26];
public void changeNumber(String number, String numberChange) {
for (int i = 0; i < myEntries.length; i++){
if (myEntries[i].getNumber().equals(number)){
myEntries[i].setNumber(numberChange);
break;
}
}
}
However, I am receiving errors for my setters and getters. This does not happen when I use a straight array or straight LinkedList, as I've already got this method working for those two in two different classes.
The error messages I'm receiving for both are
The method getNumber() is undefined for the type LinkedList
and The method getNumber() is undefined for the type LinkedList
I don't see why they're undefined as when I've tried doing the same method for a straight Array and a pure LinkedList, they've handled it fine and functioned properly.
If anyone has any suggestions, I would really appreciate it.
Pay close attention to the data type you're iterating over. Because myEntries is defined as a LinkedList<Entry>[], you're pulling out an individual LinkedList<Entry> when you iterate over the array.
It really seems like you don't want the array; instead, just iterate over the list elements directly:
LinkedList<Entry> myEntries = new LinkedList<>();
for(Entry entry : myEntries) {
if(entry.equals(number) {
// logic
}
}
myEntries[i] returns a LinkedList this doesnt have the setNumber method. You need to get the Entry out of the list and then invoke these methods.
myEntries[i].get(index).setNumber(); or myEntries[i].getFirst().setNumber(); etc
You are trying to call your accessors/mutators (getNumber() & setNumber) on the LinkedList instance and since there is no such methods for the LinkedList you will have the reported error.
So either get access to some LinkedList item with get() method that will return an Entry object on which you can call your setter and getter:
public void changeNumber(String number, String numberChange) {
int index = 0; //not sure what this index should be in your case
for (int i = 0; i < myEntries.length; i++){
if (myEntries[i].get(index).getNumber().equals(number)){
myEntries[i].get(index).setNumber(numberChange);
break;
}
}
}
Or better if you don't need the LinkedList, may be it is worth dropping you design and only create an Array of Entry:
private Entry[] entries = new Entry[26];
Then your changeNumber() method will be eligible:
public void changeNumber(String number, String numberChange) {
for (int i = 0; i < myEntries.length; i++){
if (myEntries[i].getNumber().equals(number)){
myEntries[i].setNumber(numberChange);
break;
}
}
}
I've obtained an ArrayListof string for user inputted keywords now I'm trying to see if those keywords are present in the separate elements of another arraylist which is a catalogue of books. My code is shown below and the compiler keeps saying cannot find symbol when i compile the following code:
edit: i've changed the code as follows
ArrayList<Book> catalogue; //was defined in another class
ArrayList<String> keywords;// was created in a driver class through user input
public ArrayList<String> getTitlesContainingKeyword(String keyword){
ArrayList<String>results = new ArrayList<>();
for (int i = 0; i < keywords.size(); i ++) {
for (int j = 0; j < catalogue.size(); j ++){
if (catalogue.get(j).contains(keywords.get(i))) // the contains in this line is giving the error
{
results.add(j);
}
}
}
return results;
}
it appears that the main problem is that catalogue is a book arraylist and keywords is a string arraylist
You should simply check if the keyword exists or not. Why are you using get method ? Simply pass in the String to the contains, completely.
What I mean to say is:
1. Get the keyword.
2. Check if it indeed is a valid keyword by calling the contains() on your keywords ArrayList. If it returns false, do not proceed to search the catalogues.
3. If it is, go over each catalogue and call its contains to see if the keyword is contained in that catalogue.
Your logic seems to be wrong in the code snippet.
public String getTitlesContainingKeyword(String keyword){
for (int i = 0; i < keywords.size(); i ++) {
for (int j = 0; j < catalogue.size(); j ++){
if (catalogue.get(j).contains(keywords.get(i))) {
results.add(j); // << who is results
}
}
}
return results;
}
The error is being given by the compiler because you havn't declared results in your method. Compiler doesn't know what is this results, neither do you, nor me.
If it is supposed be an ArrayList<String>, then declare and create it as such.
Change your method return type to ArrayList<String> instead of String
public ArrayList<String> getTitlesContainingKeyword(String keyword){
ArrayList<String>results = new ArrayList<>();
for (int i = 0; i < keywords.size(); i ++) {
for (String catLog: catalogue){
if (catLog.contains(keywords.get(i))) {
results.add(catLog);
}
}
}
return results;
}