The number of Contact that I input is just one when I run the program so the Number of contacts: 1 is correct, but it gives me workbook.Contact@46e5590e instead of printing out all the contacts stored inside the Contact class. Yes I do loop through the ArrayList and I also have a method inside the Contact class, the printNameAndPhone(), which prints out the name as well as the phone number but how do I incorporate the printNameAndPhone() method (located in the Contact class) inside the print() method (located inside the AddressBook class)???
Basically I'm asking how to access all the elements in the ArrayList<Contact> addressBook = new ArrayList<>();??My main class AddressBook
package addressbook;
import java.util.Scanner;
import java.util.ArrayList;
public class AddressBook {
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?
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");
I have a 2D arraylist, named as adjLists, which contains arraylists, containing values like these. Each two values are a "pair" and each third is a "flag" for the pair.
I use code above to search for specified value pairs in these lists. vertexnum is the number of "sub arraylists" in adjLists.
for (int l = 0; l < vertexnum; l++) { if (adjLists.get(l).get(0) == p.x && adjLists.get(l).get(1) == p.y) { for (int h = 0; h < adjLists.get(l).size(); h += 3) { for (int g = 0; g < vertexnum; g++) { if ((vertexSprite[g].getX() + vertexSprite[g].getWidth() / 2) == adjLists.get(l).get(h)
[Code] ....
This code is to search exact values and replace their flag in every occurences. It can not find all the occurences of the values/pair in the lists, it replaces flag value only a few time. Value of score should be incremented with plus 1 after each found occurence, but this code increments it more than the number of matches.
I have an ArrayList of tens of thousands of elements, and want to remove the items at a given set of sorted indices like {1,5,29,318,499,583}. It seems like this operation could be performed in linear time and space by first finding a cumulative sum of the shifts needed to move everything to their right positions and then shifting every item. On the other hand, removing and shifting one by one would seem to require many more shifts. Is there a way to do this in Java's ArrayList?
how to read and understand the API's. I've got an array list and I was wondering if there was a method that can randomly re arrange the elements in terms of their index positions.
I'm new to object oriented programming, my previous experiences have been in C. I was given an assignment to implement an operation to transform a list of data by creating a partitioning algoritm.
import java.util.*; public class Partition { public static void partitionWithSetGet ( List<String> theCollectionOfData ) { int i, j; while(i <= j){ int Result = theCollectionOfData.get(i).compareTo(pivot); int Result = theCollectionOfData.get(j).compareTo(pivot);
[code]....
I want to grasp the this concept in greater detail, then I can use the Java API to figure out the syntax!
im trying to make a small program where the user enters their selection choice and it prints out the corresponding information. for example, if they choose "3", i want "3. Soup de Jour" to print out
import java.util.Scanner; public class rest { public static void main(String[] args)
I have a main class AddressBook where I use ArrayList<Contact> addressBook = new ArrayList<>(); to add information such as name and phone numbers in my Contact class. How do I go about accessing all the information (names, and phone numbers) that is stored in my Contact class? What happens when I print it out is it only prints out the last input of name and phone number and not the entire collection of names and phone numbers. How do I print out the contents of ArrayList?
I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.
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; // }
Basically the class is supposed to have a static variable that keeps track of the number of companies (numberOfCompanies) that have been created and a static method that returns the value of this variable. I need to have a constructor and a method to addEmployee, a method to printCompany, and a toString method. The addEmployee method will check that there is only 1 salesperson, 2 designers, and 4 manufacturing persons at a time and will check that there are no more than 7 employees of the company.
The addEmployee method will return a String indicating what the error is (i.e. "There is already a sales person in this company") or a null if the employee was added. If attempting to name a third company, an error message will be shown. what i have so far but my print company mmethod returns null company class :in which i am obliged to use inputdialog because it says that it cannot return void when i use the showmeassage diaolog.
company class:
import java.util.ArrayList; import javax.swing.JOptionPane; public class Company { private ArrayList<Employees> list ; private static int numberOfCompanies;
[Code] ....
error message when print button is clicked:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent at javax.swing.JOptionPane.createInternalFrame(Unknown Source) at javax.swing.JOptionPane.showInternalOptionDialog(Unknown Source) at javax.swing.JOptionPane.showInternalMessageDialog(Unknown Source)
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?
I have stumbled onto a problem with ArrayLists (not sure if nested ArrayList objects would be a more accurate description) ....
As simply put as I can describe it, I have an Inventory class which creates an ArrayList of a Product superclass, which has two subclasses, PerishableProduct and ItemisedProduct.
By default, the program is meant to have a starting inventory, which is why I have added them in the constructor
public class Inventory { private List<Product> products; public Inventory() { addProduct(new Product("Frying pan", 15, 20)); addProduct(new PerishableProduct("Apple", 5.8, 30, 7)); addProduct(new ItemisedProduct("Cereal", 5.8, 0)); // this is where I am having problems. Wanting to add // objects to the ItemisedProduct's ArrayList for cereal. }
Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers.
public class ItemisedProduct extends Product { private ArrayList<String> serialNumbers = new ArrayList(); public ItemisedProduct(String name, double price, int qty) { super(name, price, qty)
[Code] .....
My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.
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?
I would like to create a java gui (with CRUD elements) that connect to MySQL server DB and display a table records (also insert, edit, delete data to table), and I want to know how create such a gui.
So I am trying to create a code that searches if a word is square free. The user inputs a word (into an array) and then the code is suppose to see if it is square free. A word being square free means that the word doesn't contain any consecutive sub words. For example, "abcabc" is not a square free word because abc is repeated, but "abcdabc" is a square free word because there is a "d" separating the "abc".
So far I have this :
import java.util.Scanner; public class A3Q2 { public static void main(String[] args) { // part (a) of the main Scanner keyboard = new Scanner(System.in);
[Code] ....
I've been trying to experiment with different ways such as checking to see if there any duplicate elements such as,
public static char isSquareFree(char[] word){ for(char i = 1; i < word.length; i++) { if(word[i] == word[i - 'a']) { System.out.println("Duplicate: " + word[i]); } } return word; } }
// 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 !!!!!!