I have two ArrayLists and I want to compare them for common elements, and based on the result I want to update the first Arraylist to only have these elements. sort of like the opposite of RemoveAll() which removes elements in common and keep the ones that are unique. so far I thought of using for loop and .contains() in case it was fault,element not present, remove from list. but I was wondering in what other ways, perhaps APIs i can use to do that?
I need to create an algorithm that finds the common element(s) in all arrays that has a signature of public Comparable[] findCommonElements(Object[] collection) that has an efficiency of at most O(knlogn), uses a query array, and accepts as input a collection of arrays. I am aware my time would be better spent learning how to use array lists and hash sets, but I am supposed to use concepts already covered, and these have not been.
I feel like this code should work, but it is returning null for the array of common elements. Which means it obviously is not working correctly. I am also likely going to need implementing the sort algorithm, but I wanted to get the part of finding the common elements set first.
public class CommonElements2<T extends Comparable<T>> { Comparable[] tempArr; Comparable[] common; Comparable[] queryArray; /* sort algorithm goes here */ public Comparable[] findCommonElements(Object[] collections)
In attempting to find the greatest common divisor (gcd) of two integers, n1 and n2, whereas k is a possible gcd and gcd is initialized to 1, the following code has been entered:
for (int k = 2; k <= n1 && k <= n2; k++) { if ( n1 % 2 == 0 && n2 % 2 == 0) gcd= k; }
When asked to change the previous line of code to this:
for (int k = 2; k <= n1 / 2 && k < n2 / 2; k++){
the questions states this revision is wrong and to find the reason.....well, I've changed it, entered "3" (per the answer key) for n1 and n2....
now I can see logically where k (2 in this example) is not <= n1/2, which is 3/2 or 1, since we're dealing w/integers, yet when I compile and run, my answer is indeed, gcd = 1. However, since this is a Boolean expression where && is being used, since the first portion evaluates to "false", the 2nd portion isn't executed and thus my result of 1?...... loops are throwing me for one, for sure....
I'm trying to make a simple Caesar cipher that takes in a String as the first argument and a integer shift as the second argument. Namely, there appears to be a problem with the loop and how I have declared the array - I want the converted characters to be put into a new array called newCharacterArray, converted back to a String and displayed in the command prompt window.
public class Caesar { public static void main(String inString, int k) { System.out.println("String: " + inString); char inStringArray[] = inString.toCharArray();
I have to recursively find a given string in a file. I HAVE to use the LineNumberReader class, and the output would be like so:
Line#Found : the string of the whole line
This is the code I've written:
public String findGivenString(String givenString, int currentLineNumber) { LineNumberReader lnr = null; try { lnr = new LineNumberReader(new FileReader(getFile()), 4096); lnr.setLineNumber(currentLineNumber); String s = lnr.readLine().toLowerCase();
[Code] ....
I messed around with a bit, and it doesn't change to the new set line. Though the line number is incrementing! So it just keeps checking the first line of the file over and over again, which is why it can't find the given string. Which also throws the StackOverFlow exception I'm getting.
Here's the output if I remove the comment from the System.out...:
String @ that Line# 1: package banking; String @ that Line# 2: package banking; String @ that Line# 3: package banking; String @ that Line# 4: package banking; ....
So you see it keeps checking the same line even though the line number IS incrementing.
SimpleDotComTestDrive.java:8: error: cannot find symbol String result = dot.checkYourself(userGuess); ^ symbol: method checkYourself(String) location: variable dot of type SimpleDotCom 1 error
Code:
public class SimpleDotCom { int[] locationCells; int numOfHits = 0; public void setLocationCells(int[] locs) { locationCells = locs; } public String checkYourSelf(String stringGuess) { int guess = Integer.parseInt(stringGuess); String result = "miss"; for (int cell : locationCells) {
I have got a pretty good framework working for my hangman game so far, however I am quite stumped on how to find multiple instances of the same letter in a single string. I am using indexOf to find the instance of a character in a string, but it only returns the first instance. So if the words(s) contain(s) multiple instances it doesn't register the rest.
public static void main(String[] args) { String word = "crazy horse"; String answer=""; String space=" "; String dash="-";
I have included split() to put a string read from a given file into indexed array. Looking for a word position (not char position number in addition to the line number I have already written. Line number works fine, however word position isn't quite right.Below is my code:
import java.io.*; public class Word implements Comparable, TreeComparable{ String word; int count; int wordpos; ObjectList lines; private SuperOutput so;
Which of these is not a real differentiator for programming languages:
a) Object-oriented / Process-Oriented b) Interactive / Automated c) Interpreted / Compiled d) Strongly-Typed / Weakly-Typed e) All of the above f) B and C g) B and D
Almost all support OOP, Interactive/Automated, Interpreted/Compiled but not sure about Strongly typed/Weakly typed.
I have developed a web portal using jsp and struts 2. I have approximately 10 JSP pages which looks exactly the same and have two text areas and two hidden fields. All 10 pages are exactly the same except for hidden field value. Can't i have a single common jsp page. How can i achieve it. A sample page i am attaching...
I am using the following regex - [a-zA-Z0-9]{9,18} which means I can use alphabets and numbers with minimum length as 9 and maximum length as 18.It should not take special characters.
It takes values like ADV0098890 etc. but it is also taking ADV0098890[] which is wrong.
when a new object is created in Java it follows the idiom:
Object obj = new Object(); where the Object() constructor matches the object type Object.
But what if it doesn't? I understand from the Oracle Docs on creating objects and polymorphism that the constructor must be in that object's class or one of its subclasses. However, suppose we wanted to declare a new stack. My first instinct would be:
Stack s1 = new Stack(); But I assume it's valid to do it this way, too:
Object s2 = new Stack(); // Is there a difference here? What are we really saying about s2? I'm guessing s2 is simply an empty stack, but only has access to the Object class methods? I'm not sure why someone would ever do this, but I want to solidify my understanding of the Java hierarchy. Are there really any circumstances where someone would use a subclass's constructor when creating a new object?
I have two different "business objects", and they have multiple attributes in common(around 25 I believe, all of which are simply a String). Basically, these objects are used for documentation purposes on the same file.
The program can choose to update a given Document at any point in time, even if changes haven't been made to existing version. So what I'm trying to do, is check to see if these attributes differ any between the two files(the exisitng copy, and the new request). If so, I'll update...else I simply throw out the request. The workload can be rather intense at times so I don't want to bog down the system anymore then necessary.
Simply pulling down every attribute for each and comparing seems like a lot of overhead, any more efficient way to achieve these results?
The final map would have all the values from Map A as a key and the values from Map B as values in the Final Map. Is there a way to do this using Java?
I have done one program, that calculates the Least Common Multiple. The idea is to use WindowsBuilder on Ecplise in order to run it in a separate window. But when I started nothing happens. The code is:
Just wondering what could I do to make it happen. One thing came up on my mind - this is that need to connect the button with the function of the method, but still not sure will work.
I am working with Netbeans 8.0 and JSF2.2. I am trying to create a web site that has a common header, footer, and menu. The only part that would be dynamic is the content. So here goes...
1) I need a common template that has 4 parts, Header, Footer, Menu, Content 2) The header, footer and menu are to be in a separate files of which are called from the main template 3) The content will change based on the menu item changed 4) The menu has to change the content section only
I know how to do this in HTML but I am trying to set up some thing in JSF to learn more on JSF pages. All I am looking for is an example that I can follow along with having the multiple pages as well having the menu change the content (I have done a ton of internet searching but nothing really fits the bill).
This is the code I have currently.
Index (Main Template) <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:h="http://xmlns.jcp.org/jsf/html">