Reversing Elements In The List

Feb 14, 2014

i have the following code to reverse the elements of the list but i only print the last element each time

public void reverse(OrderedList<T> lister) {
OrderedList<T> listerCopy = lister;
for(int i=0;i<5;++i) {
T removed=removeFromBack(listerCopy);
System.out.printf("%s ", removed);

[Code] .....

View Replies


ADVERTISEMENT

Adding Elements To A List

Feb 6, 2014

Here is a small part of my program

Java Code:

// for every attribute not mentioned in the rule r
enumAtt = ruleE.enumerateAttributes();
while (enumAtt.hasMoreElements()) {
Attribute attr = (Attribute) enumAtt.nextElement();
if (isMentionedIn(attr, r.m_test)) {continue;}
int M = attr.numValues();

[Code] ....

It repeats only the last rule , why is that !!!!!!

View Replies View Related

How Does CompareTo Work If There Are More Than 2 Elements In List

Jan 8, 2015

Lets suppose that I pass to the sort method a list of 2 objects of the same class (which implements Comparable interface). I read (in head first java) that one object is compared relative to another with one object calling the CompareTo() while the other object being passed as a parameter to the same method. Now am I safe in assuming that the first object in the list calls the method with the second object being passed as a parameter.And also how does the CompareTo() work if there are more than 2 elements in the list. Which objcet calls the method and which is passed as a parameter?

View Replies View Related

Associating Properties With Array List Elements?

Dec 17, 2014

In my project I need to create a pizza ordering system. I have created a array list of pizza toppings and each pizza topping must have a cost property associated with it, then the user can pick the toppings they want and the cost of each topping will be added up and the total cost of the whole pizza will be displayed. But I don't know how to associate a cost with each topping. I have been told that I can access the cost through a get/set method and store it in a private member variable. But I don't know how to do this?

I've tried to do it, but it hasn't worked. This is the code

public void setCost( double cost){ //my get/set method
this.cost = cost;
}
public double getCost(){
double price = 0;
return price;

[Code] .....

But there is an error on each of the ingrediantCost method lines, saying

The primitive type double of cost does not have a field Pepperoni

...and the same for chicken, ham and onion.

View Replies View Related

Reusing Entered List Of Array Elements

Sep 11, 2014

If you see in the code below where I print "Would you like to use previous key" I need to be able to save the previous key answers to be used again.. not sure how to go about doing this..

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] studentName = new String[20];
int[] studentAverage = new int[20];
Scanner input = new Scanner(System.in);

[Code] .....

View Replies View Related

Elements Of Circular Doubly Linked List

Feb 12, 2015

For the program I have to create, I need to be able to work with the elements of a circular doubly linked list to do the following:

Find an element in the list (specified as an argument)
Insert an element after a specified element (specified as an argument)
Delete an element (specified as an argument)
Display the elements in the list

I can insert elements and display the elements, but I can't figure out how to search for an element, insert an element after a specified element, or delete an element.

What I am having trouble with is passing the elements in the list as arguments for searching, deleting, inserting after an element.

public class doublyLinkedList {
private Node pointer;
private Node dLL;
private int count;

[Code] .....

View Replies View Related

JSF :: Create A List Or Array Of Child Elements To Feed To Java Bean

Feb 5, 2014

I have a project I am currently working on using Primefaces and MySQL. I have a page that will display information queried from the database. I have textfields that will display the data from a given record. But I want to ensure that the displayed data cannot be altered. I decided on disabling the textfields unless the create or edit button were clicked. At which point all textfields will be enabled for editing, and cleared if create button was clicked. I want to push the disabling/enabling of the elements back to a managed bean, but I want to have the bean take in two lists or arrays, one containing all elements by ID and one containing a list of exempt elements. Is there a way I can create a List or Array of the ID's that I can send to the bean? I know little in Javascript and previous attempts to implement it have met with little success.

A short example of the code:

<p:tabView>
<p:tab id=pmDBTab title="PM Database Tab">
<p:panelGrid id="projectInfo">
<p:row id="rowOne">
<p:column id="r1c1" colspan="2">
<p:outputLabel id="searchLabel" for="searchField" value="Search: " style="font-weight: bolder"/>

[Code] ....

I want to create a list that would contain all and another that would contain the exemptions such as searchField, searchCriteria, searchButton, projectList and viewButton. The actual exemption list is far smaller than the list of all children. I have found sources that can disable the elements with a java bean, but I have not been able to find one that would allow me to define those that should and should not be disabled, then vice versa for enabling.

View Replies View Related

Declare Array Of 50 Elements Of Type Double - Print Output 10 Elements Per Line

Feb 5, 2015

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class array
{
public static void main(String[] args)

[Code] ...

Is there a way to write this, where, alpha is one array.

Write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed.

If I have an array of 50 integers, can I break that to read in lines of 10?

View Replies View Related

Reversing Numbers Entered (in Java)

Mar 29, 2014

How to calculate the minimum and the maximum in the same program.
 
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int integers;
int[] numbers;
numbers = new int[10];
int max;
int min;
 
[Code] ....

My result when I input numbers are always the following:

"Numbers in reverse order are: 9, Numbers in reverse order are: 8,
Numbers in reverse order are: 7, Numbers in reverse order are: 6
Numbers in reverse order are: 5, Numbers in reverse order are: 4,
Numbers in reverse order are: 3, Numbers in reverse order are: 2,
Numbers in reverse order are: 1, Numbers in reverse order are: 0."

View Replies View Related

Reversing Numbers (User Input)

Dec 11, 2014

I am writing a program to reverse a user inputted number (Example - 54321 is 12345) The code works great minus the fact that if I input a number that begins with zero, the output will drop the zero. Would it make more sense to just set this up using strings?

import java.util.*;
public class Week7_Programming_Problem
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int inputNum, outputNum;

[Code] ....

View Replies View Related

Java Code Using Recursion - Reversing Sentence

Mar 15, 2015

So I have to design a program that takes a word and reverses the order of it. The exact directions are:Write a recursive method void reverse() that reverses a sentence. For example:

[code = Java]
Sentence greeting = new Sentence("Hello!");
greeting.reverse();
System.out.println(greeting.getText());
[/code]
prints the string "!olleH".

Implement a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and combining the two. So basically my biggest issue is my method to actually reverse the word.

public class Sentance
{
private String Word;
public Sentance (String aWord)
{
Word = aWord;

[code]....

View Replies View Related

Taking String That Contain Sentence And Reversing The Words

Sep 5, 2014

I am not very comfortable with Strings in Java. The problem I am facing is, taking a string that contains a sentence and reversing the words. Example would be, "Hi I am Bob" and changing it to "Bob am I Hi". Then returning the String.

My initial thoughts were to change the string into a character array and then manually doing the work with loops and tedious comparison statements. I quickly realized that there must be a better way but I am not very familiar with strings in Java to know what a more sufficient way would be.

View Replies View Related

Iteratively And Recursively Summing And Reversing Array Of Longs

Mar 23, 2015

My program is supposed to use recursion and iteration to sum and reverse elements in an array of Longs. The array is supposed to be 1000000 cells and I am supposed to compute the average times of 99999 trials. When I try to run the program, it keeps saying running.... but doesnt ever do anything.

public class PA2Delegate {
private long[] start;
private long[] end;
private Long reversal;
private Long sum = (long)(int)0;

[Code] .....

View Replies View Related

Add Elements To 2D Array Without Losing Elements

Jan 12, 2015

I am trying to make a 2d array that keeps track of comparison counts. what I have so far works ok but writes over the previous elements with 0. can't seem to find where I am re-initializing the previous elements.

//this is one of my fill sort arrays

public void fillSelectionArray(int index, long countSum) {
//rand = new Random( );
//for ( int i = 0; i < listsize; i++) {
selectionList[ index -1] = countSum;
// }

[Code] ....

I know I am missing something just not sure what.

View Replies View Related

Sort Linked List Through The Nodes Of List - Data Of Calling Object

Feb 14, 2014

I have some class called sorted to sort the linked list through the nodes of the list. and other class to test this ability, i made object of the sort class called "list1" and insert the values to the linked list.

If i make other object called "list2" and want to merge those two lists by using method merge in sort class. And wrote code of

list1.merge(list2);

How can the merge method in sort class know the values of list1 that called it as this object is created in other class.

View Replies View Related

How To Append New Entry In A List Of 100,000 Names Without Iterating List Each Time

Apr 22, 2015

I have a list of 100,000 + names and I need to append to that list another 100,000 names. Each name must be unique. Currently I iterate through the entire list to be sure the name does not exist. As you can imagine this is very slow. I am new to Java and I am maintaining a 15+ year old product. Is there a better way to check for an existing name?

View Replies View Related

Unable To Filter A List And Then Assign Results To New List

Dec 30, 2014

I receive a java.lang.NumberFormatException: For input string: ""DepDelayMinutes"" error when trying to filter a list and then assign the results to a new list.

I have edited the code so it is an int that throws the exception so it isn't the presence of a string that is causing the error - java.lang.NumberFormatException: For input string: ""0914"" .

I believe the issue is because of the two sets of double quotes but I do not understand how they came about. The original dataset does not have any quotes whatsoever.

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class FilterObjects extends Thread{

[Code]...

View Replies View Related

Iterating Over List And Inserting Each Element From List Into BST?

Jul 5, 2014

Suppose i have given a List<Intervals> list; I am iterating over list and inserting each element from list into BST, if time require to insert into BST is logn then what is the total time require to insert all the elements into tree ?

logn or nlogn ?

View Replies View Related

Linked List Implementation Of List Interface?

Oct 5, 2013

So we have an assignment regarding a linked list implementation of a given list interface.

In my list interface, the method contains(T anEntry) is defined.

In the LList implementation, contains is already implemented as part of getting the core methods in.

Now I am being tasked with the following:

Provide a second implementation of the method contains2(T anEntry) that calls a private recursive method

Private boolean contains (T anEntry, Node startNode) that returns whether the list that starts at startNode contains the entry anEntry.

I've written the private recursive method already. That's not an issue (at least not right now).

But what I don't understand is how startNode is supposed to be populated when this private contains method is called from the public contains2 method? contains2 only takes one parameter: anEntry. the private method takes two parameters: anEntry and startNode. How am i supposed to provide startNode when I am calling contains2?

View Replies View Related

Add To List With ID Makes List Upside Down

Feb 14, 2015

I saved the content of an ArrayList<SomeClass> to a file

I write each serialised object to the file by adding to each an id that is created with list.indexOf(object) (and saved before each object)

So I can repopulated the list with SomeClass objects in the same order as they were and once I reload the file, I do list.add(id,object)...

Well my list is populated upside down, I mean the first object is the last and the other way around, If i remove the id when I do the add(object)...

View Replies View Related

How To Add Elements To A Map

Mar 13, 2015

I have a requirement to add elements to a map. But for, First iteration it inserts fine and for next iteration elements are overrided.

I am just wondering if there were append() in map or in any collections?

View Replies View Related

Why Not Getting All The Elements In TreeSet

Aug 1, 2014

public void createPersons() {
Random rand = new Random();
for(int index = 0; index < persons.length; index++) {
Person person = new Person(rand.nextInt(maxAge), persons[index]);
setOfPersons.add(person);

[Code] ....

setOfPersons is a TreeSet, persons[] is an array of strings that contains 10 names. Why printPersons() prints only the half names of setOfPersons?

Note. I do get the size of the TreeSet as 10...

View Replies View Related

Sum Of Array Elements?

May 4, 2015

What is the sum of the array ar elements after executing the following code?

int[] ar = {1,2,3,4,5};
for (int i=0; i<ar.length-1; i++)
if (i%2==0) ar[i]=i;

My professor says the answer is 12 i dont know how he got that answer

View Replies View Related

Max And Min Of Elements In Array

May 17, 2014

I cant get this code to work if wont find the right min and range and its printing out my max three times . This is what I got

public static void main(String[] args)
{
double [] Rainfall = new double[12];
Rainfall[0] = 77.4;
Rainfall[1] = 56;
Rainfall[2] = 61.8;
Rainfall[3] = 62.1;
Rainfall[4] = 57.1;

[Code] ....

View Replies View Related

How To Copy Elements Of One Array Into Another

Mar 29, 2015

I have two arrays

private ReservedBook[] reservedBooks;
private LibrarySystem[] libraryBooks;

The library array has two books and I want to copy one of them to the reserved books when you type in the ISBN

public void borrowBook(String ISBN)
{
int i = 0;
if(numberOfBooks < MAX_BOOKS-1)
{
if(libraryBooks[i].getBookISBN().equals(ISBN))
{
for(i=0;i<MAX_BOOKS-1;i++)
reservedBooks[i] = libraryBooks[i];
}
else System.out.println("There is no such book");
}
else System.out.println("You have reached the maximum number of allowed books");
}

It shows me error: incompatible types - LibrarySystem cannot be converted into ReservedBook. How can I fix it?

View Replies View Related

Removing Elements From ArrayList

Aug 24, 2014

Referring Code 1, the book says line 16 of the code removes the element "Three" but line 17 does not remove the element "Four" because of Statement 1. The question is does remove(Object o) method invoke the == or the equals method because statement 1 and 2 seem to be in conflict

Statement 1:

Two objects are equal if their object references point to the same object. (which is nothing but definition of ==)

Statement 2:

The author refers to Statement 1 and says "As mentioned earlier, the method remove compares the objects for equality before removing it from ArrayList by calling method equals."

Java Code:

import java.util.ArrayList;
public class DeleteElementsFromArrayList {
public static void main(String args[]) {
ArrayList<StringBuilder> myArrList = new ArrayList<>();
StringBuilder sb1 = new StringBuilder("One");

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved