So I am learning HashMaps/Arraylists and I can't really understand the diamond operator or what it's for. Take the following code for example: Why could we not just do this without the diamond?
Java Code:
import java.util.HashMap;
class Untitled {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();
I am currently trying to implement a hashmap from a class i currently have that is called 'Paper'
My paper class consists of:
String Title; String[] author;
What I am wondering, is if there is anyway I can call these seperate attributes to be both the key and the value of the hashmap. Currently I cannot see a way to call individual values, only the class itself.
my query is like: "i want to create hashmaps where the inner hashmap contains the key as firstname and value as a Class Student which contains parameters firstname, lastname,etc. I need this hashmap to be accessed by other objects of different classes".
As we give the values by Scanner, it should be Stored in Hashmap and how those values can be accessed by other object classes?How does the Logic works? What my next step?
I'm learning Java using BlueJ, I have made a class that has a HashMap of (String, String) that contains an the make of a car and the model.
I want a method to return a collection of all the keys that satisfy a condition, like if someone wants to find what make a certain model is. I know it requires a loop, just not too sure how to write the loop to satisfy the condition.
I am new to java and i am trying to make a Java application which prints a diamond in a square grid of dots whose side length is input to the application.When you run the code is should be like this:
..*.. .*.*. *...* .*.*. ..*..
My java code print this:
..*.. .***. ***** .***. ..*..
Here is my code:
class Main { public static void main(String args[]) { System.out.println("#Enter size of Diamond :"); int longestRow = BIO.getInt(); for(int row=1 ; row<=longestRow ; ++row)
Write a program that reads an integer and displays, using asterisks, a filled diamond of the given side length. For example, if the side length is 4 the program should display.
* *** ***** ******* ***** *** *
I have it where it displays the top half of the diamond. But i cannot figure out how to get it to draw the bottom half.
import java.util.*; public class E616 { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter number of rows. "); int N=input.nextInt();
I am new to learning Java and I am trying to print an unfilled diamond using asterisks. The height of the diamond has to be based on user input. My code currently prints out a filled diamond based on user input but I cannot figure out how to print an unfilled one. Every time I change one of the loops it messes something else up.
[public static void diamond(){ int i = 0; int j = 0; int k = 0; int height = 0;
I wrote this (for the variable name, this is just a small part of my program and I'm trying to get the pattern right so I didn't mind my variables at the moment):
Java Code:
public class NewFile{ public static void main(String []args){ int k = 0; for (int i=1 ; i<=5 ; i++) { { for (int h=2 ; h >= i ; h--)
I realize I should divide the code into a lower and upper triangle using two loops. However, I don't know how to break the first part. I did find the "trend" but I don't see how to implement it.
I have to make ask for 2 houses or more (INSTANSED CLASS, with 2 ints ) with the same size(int ), but i they cant use the same door number(int). how do i check in my ArrayList that they have different numbers while not caring about different size? .
The ArrayList is in a class called system. I dont know if i should try to Override the equal or try something else.
>Suppose that aList and bList are instances of java.util.ArrayList. Use two iterators to find and display all the objects that are common to both lists. Do not alter the contents of either list. Write the segment of code assuming that the objects are of type String.
If my understanding is correct, I need to create two Iterators (one for aList and one for bList) to compare both ArrayLists and I should output any value that appears in both lists.
This is my attempt at it:
import java.util.ArrayList; public class Test { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); ArrayList<String> bList = new ArrayList<String>();
aList.add("One"); aList.add("Two");
[Code] ....
The output:
`One`
`Two`
It stops there. It's clear that my loop is wrong, but I don't really know how to go about fixing it.
I have two arraylists. One is personalContactList. The other is businessContactList.
1. I want to take user input that references an object attribute (int), 2. Use it to determine which object is referenced, 3. Find the "type" attribute of that object, 4. Determine which arraylist the object belongs to, based on that type, 5. Use the if else/statement to print out some attributes that depends on which arraylist the object is in.
I believe the code successfully does 1-3, and probably 4. But there is a hangup on 5. I get an indexoutofbounds execption. Which I'm not sure I understand very well.
Here is the code--it's part of a switch statement:
case "c": { //some code here prints out a list boolean b = false; int g; while (b == false) { Scanner detailScanner = new Scanner(System.in); System.out.print("Enter a number to see details: ");
I'm new to java and up until now whenever I have created an object it has been via the BlueJ interface. I would make an object manually and then manipulate it with my methods.
I have made a basic program which adds objects into arraylists via the BlueJ interface. However I now need to prompt a user to enter a string which should create the specific object and add to its ArrayList.
Is there a way to create objects without manual specifying the parameters? I can provide some of my code if it is needed I am trying to make a crude version of Plant vs Zombies for an assignment.
I am aware of the scanner class but I only know how to prompt a user but I feel like if I can figure a way to create the objects I can dynamically I can associate the string inputs to the proper arraylists.
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 have this piece of code, which adds marks into 2 different arrayLists, one for homework marks, and one for examination marks..
ArrayList<Double> homeworkMark = new ArrayList<Double>(); ArrayList<Double> examinationMark = new ArrayList<Double>(); boolean markCheck = true; do{ // the purpose of this try catch is to make sure that the entered mark is a valid number. If the program encounters an exception markCheck will become true and the loop will begin again asking the user to enter the marks. If the marks are all encountered correctly markCheck will be false and the loop will end.
try{ for (int i = 1; i <= amountAssignment; i++ ) { sc.reset(); System.out.print("Homework Mark for Assignment " +(i) + ": "); homeworkMark.add(sc.nextDouble()); System.out.print("Examination Mark for Assignment " + (i) + ": "); examinationMark.add(sc.nextDouble()); markCheck = false;
So I have a program where you can log in and add/remove friends to and from the friends arraylist. Also I can like a certain thing and that thing will be stored into the likes arraylist. I'm asked to make undo and redo options for whichever action I do.I want to add apple as a friend. After that when I select the undo option, I can undo that action so apple wouldn't be my friend. How I can approach this with a Command Pattern when the input is whatever name or word I inputted to store into the friends arraylist?
I did some research and found that using a command pattern could be my best bet since this has to be done under the Facebook Class I already have. I decided to add parts of what I have ...
In the driver program
Facebook facebook1 = new Facebook();
if (userInput == 6) { System.out.println("Login"); String operand1 = getOperand("What is the Username? "); String operand2 = getOperand("What is the Password? "); System.out.println("Enter a friend to be added. "); String operand3 = getOperand("What is the Username? "); facebook1.friend(operand3);
I have been doing a program that allows the user to input students names and numerical grade values. I have already completed 3/4 of the program but am stuck on the bubblesort method as this is what it requires:
The program must have an indefinite loop, which prompts the user to select a sorting criterion or to end the program, and must also use bubble sort. Note: The user may either select name or grade as the sorting criteria. The program must use bubble sort to sort that data according to the specific criteria and then use another loop to display the data. This process must continue until the user ends the program.
My problem is, while i can easily do the bubblesort, where to look on how to link elements of the two arraylists I'm using (one for grades, one for names) so that when the user decides what sort they want, the individual's name and grade stays together.
[URL] I made a program that takes 2 stock values by URL tickers, and now I need it to draw a graph with the values for the 2 stocks, so they can be compared visually. So far my idea was to create 2 arraylists that consist of the values for each stock, so that I can draw the graph so that x always move with 1 step, and y0 is the first coordinate, and y1 is the second, and after that y1 is the first, y2 second etc.
I have come across an issue with arraylists. I am writing a text based RPG game as something to start with ...
Initially I had a single zone which was all stored in an arraylist and everything was working in regards to the player moving around. The problem I now have is how to add further zones to my game. Ideally I would like an arraylist for each zone, and would use the below to create each arraylist
public static ArrayList<RoomsClass> castleMap = new ArrayList<>();
The problem I now have is how to handle the player moving, initially with a single zone/arraylist I could reference that arraylist directly
public void findRoomCoords(int ID) { for (int i = 0; i < castleMap.size(); i++) { if (castleMap.get(i).roomID.equals(ID)) { PLAYER.setCurrentRoomZone(castleMap.get(i).roomZone); PLAYER.setCurrentRoomX(castleMap.get(i).roomX); PLAYER.setCurrentRoomY(castleMap.get(i).roomY); PLAYER.setCurrentRoomZ(castleMap.get(i).roomZ); } } }
My initial thought was to use a getter/setter to remove the reference of castleMap from my movement code in order to access different arraylists, however this is where things have fallen over, I can't seem to work out how to get the arraylist name to change, depending on the outcome of the setZoneMap() method.
public void setZoneMap() { switch (PLAYER.getCurrentRoomZone()) { case 0: { zoneMap = Castle.castleMap; break;
I have an issue with an IndexOutOfBoundsException. I am trying to populate a db from another using arraylists and arrays. I can get the data but the program fails when trying to run the inserts. I am trying to perform SQL in batches of 5. I have added a comment to the failing line.
/*Set in code at beginning*/ ArrayList<String[]> privacyList = new ArrayList<String[]>(); ArrayList<String[]> statementBuffer = new ArrayList<String[]>();
/*Some sql is performed and the following String array is populated*/ while (rs.next()) { String[] row = new String[55]; resultSetIsEmpty = false; row[0] = rs.getString("ID");
I have a class with static ArrayLists to hold objects such as Members,Players etc.I want to save the class with the arrays so as to reload them again and hold onto the list of objects within those ArrayLists.
The ArrayClass
import java.io.Serializable; import java.util.ArrayList; public class ArrayClass implements Serializable {
[code]....
The arrays within the ArrayClass are empty when i reload the application.I cant tell if the arrays are being properly saved or is it in the reloading from file???