Accessing ArrayList Elements (Integer / String) And Put Them Into Hashtable
Mar 4, 2014
I have a data structure such as: ArrayList<ArrayList<Integer,String>
The data looks as [[1,2,3] [3,4] [99,98,40,32,45,65,1]]
I am trying to access each element and put them into a hashtable such as:
Hashtable<Integer,String>
With what I am doing below I am getting an out of bound error but can't see any other way of accessing the element
public static void hash(ArrayList<ArrayList<String>> list)
{
for(int i = 0; i < list.size(); i++)
{
int cnt = 0;
for(int y = 0; y < list.size(); y++)
{
if (! hashMap.containsValue(list.get(i).get(y)))
{
hashMap.put(cnt, list.get(i).get(y));
cnt++;
}
...
View Replies
ADVERTISEMENT
May 5, 2014
I'm having an issues with adding integer values to a string list. The question is asking me "the method should iterate over runners, and for each runner generate a random number between 90 and 180 (inclusive) which should be used to set the time (in minutes) for that runner."
I have been able to get the random number and iterating over the runner arraylist but I haven't been able to figure out how to add the values generated into the runners list. I am also using BlueJ.
Here's the whole code I have at the moment:
import java.util.*;
import java.io.*;
import ou.*;
import java.util.Random;
/**
* Write a description of class MarathonAdmin here.
*/
public class MarathonAdmin {
// instance variables - replace the example below with your own
[Code] .....
View Replies
View Related
Aug 27, 2014
i am interested to add integer objects and String objects into any collection object ..... while iterating the collection object i am not interested to do any type cast in java
View Replies
View Related
Apr 29, 2014
I'm having a problem implementing double hashing when I insert a value into the table i get this for the output:
Insert []. Is there something I'm doing wrong? I'm not getting any errors
public static int size = 0;
public static ArrayList<Integer>[] a;
public static void interactiveMode(Scanner input) {
int menuChoice = 0;
do {
System.out.println("
Interactive Mode:");
[code]....
View Replies
View Related
Apr 26, 2014
I'm having a problem with double hashing, its supposed to have the user insert an integer into the table and the integer is hashed into the table by the mod of the size of the table so say for instance table size 10 I insert 89 that inserts into 9 because 89%10=9 then i insert 18 that goes into 8 but then once i insert 69 its supposed to collide with index 9 and then uses 7-(69%7) for prime number double hashing...but it just goes into index 9 instead so in 9 it puts 89,69...there's no errors just the wrong output
System.out.print("Enter size of table: ");
int n = input.nextInt();
size=n;
a = new ArrayList[size];
for (int i = 0; i < size; i++) {
a[i] = new ArrayList<Integer>();
[code]....
View Replies
View Related
Oct 19, 2014
What I want to do is this, this is my first class:
public class Footballer {
int goals;
String surname= "";
String team="";
private static int counter=0;
private int dres;
}
(this is just the header of the class... And this is my second class, which contains an ArrayList of the first class:
public class FootballTeam{
String teamname="";
String league="";
ArrayList<Footballer> f;
}
And this is my third class which contains an ArrayList of the second class:
public class FootballLeague{
String leaguename="";
ArrayList<FootballTeam> ft;
}
What I want to do is, know how many of footballers are there in the league? Meaning how many of "f"s are in the "ft"... I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.
I'm trying this method to get the size of the array in the 2nd class, from the 3rd class (containing an ArrayList of classes of 2nd class, but no luck:
int counter=0;
for(int i=0;i<this.ft.size();i++) {
counter+=this.ft[i].f.size();
}
I'm getting this: Array required, but ArrayList<FootballTeam> found ---
View Replies
View Related
Nov 27, 2014
I'm a total newbie to Java, and until now all I've done was draw some shapes and flags. I'm struggling to understand the code I've been given. I need to access values stored in an ArrayList within another class. Here are the two classes Seat and Mandate:
package wtf2;
import java.util.*;
public class Seat {
public int index;
public String place;
public int electorate;
[Code] ....
The main class contains code that feeds data from 2 text files into Seat and Mandate. From there I managed to access the date in Seat (at the end):
package wtf2;
import java.io.*;
import java.util.*;
public class CW2 {
public static void main(String[] args)throws Exception {
[Code] ....
Now,instead of getting just the mp for Edinburgh South I need to get the vote values, compare them to each other, take the second biggest and display the associate party value. How to access data from that Array to get started at least.
View Replies
View Related
Oct 19, 2014
What I want to do is this, this is my first class:
Java Code:
public class Footballer {
int goals;
String surname= "";
String team="";
private static int counter=0;
private int dres;
} mh_sh_highlight_all('java');
(this is just the header of the class, just how it looks)...
And this is my second class, which contains an ArrayList of the first class:
Java Code:
public class FootballTeam{
String teamname="";
String league="";
ArrayList<Footballer> f;
} mh_sh_highlight_all('java');
And this is my third class which contains an ArrayList of the second clas:
Java Code: public class FootballLeague{
String leaguename="";
ArrayList<FootballTeam> ft;
} mh_sh_highlight_all('java');
What I want to do is, know how many of footballers are there in the league? Meaning how many of "f"s are in the "ft"... I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.
View Replies
View Related
Oct 14, 2014
Scanner in = new Scanner(System.in);
ArrayList<rand> selectedRand = new ArrayList<Rand>();
selectedRand.add(new Rand(in.nextLine()));
I have created the most minimal code for creating an array list. I was wondering what the basic syntax of accessing objects methods that are within an Array List. So if I was to trying and get a method such as [.returnValue,] how would this look within a Rand object that is declared in a Array List Since you cannot simply declare a new Rand object and say:
newRandObject.returnValue();
And you must go through the actual slotted portion of the array list. I have searched the web and my text book for an example however none are provided.
View Replies
View Related
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
May 11, 2015
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.
[278, 106, 0, 397, 36, 0, 59, 208, 0, 366, 221, 0]
[366, 221, 0, 397, 36, 0, 132, 390, 0, 278, 106, 0, 295, 0, 0]
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.
View Replies
View Related
Oct 21, 2014
1. Create a program that will return the maximum and minimum numbers in the elements of ONE-dimensional integer array. Save it as MaxMin_OneDim.java
2. Create a program that will return the maximum and minimum numbers in the elements of each row in a TWO-dimensional integer array. Save it as MaxMin_TwoDim.java
3. Write a program PrintPattern which prompt a user to enter a number and prints the following patterns using nested loops (assumed user entered number is 8 output is:)
1 .... 87654321
12 .... 7654321
123 .... 654321
1234 .... 54321
12345 .... 4321
123456 .... 321
1234567 .... 21
12345678 .... 1
(Without the dots, i just put them to give spaces)
View Replies
View Related
Jun 13, 2014
How to input 10 integer elements in an array & sort them in desc. order using the bubble sort technique.
View Replies
View Related
Dec 27, 2014
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?
View Replies
View Related
Oct 5, 2014
When this program runs it gives me
workbook.Contact@46e5590e
Number of contact: 1
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 {
[code]....
View Replies
View Related
Feb 24, 2015
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.
View Replies
View Related
Feb 18, 2014
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!
View Replies
View Related
Feb 24, 2014
I've tried a couple ways to do it, and they don't work. I'm aiming for functionality like I got with the regular for loop, but from an enhanced for loop. Is this simply beyond the scope of an enhanced for loop, or am I just not getting the right syntax?
TestObject to1 = new TestObject("first", 11);
TestObject to2 = new TestObject("second", 12);
TestObject to3 = new TestObject("third", 13);
TestObject to4 = new TestObject("fourth", 14);
TestObject to5 = new TestObject();
List<TestObject> testList;
testList = new ArrayList<TestObject>();
[code]....
The TestObject class is simply an int and a String, with getters getInt and getString. It all works fine with the regular for loop.
edit: I should probably mention that I know what I have in the enhanced for loop now will only display the class name and the hash. I've tried adding the .getString and .getInt, and tried a few other ways to make it work. I just reverted to this because it compiles and runs
View Replies
View Related
Nov 4, 2014
I am trying to create a method for my "ticketmachine" object that counts ticket objects in an arraylist of "ticket" objects with a specified field "route". The method works if i change the field type of the ticket class to public, however according to the homework task "route" of the ticket class needs to remain a private field.
Here is my code for the method.
public int countTickets(String route) //HAD TO CHANGE Ticket class FIELD "ROUTE" TO PUBLIC!!!!
{
int index = 0; //returns the number of Ticket objects in the machine with the specified "route".
int ticketCounter = 0;
while (index < tickets.size())
[Code] ....
Is my general approach to the problem wrong? also is there a way to access a private field?
View Replies
View Related
Oct 14, 2014
I am very new to coding and I am struggling with one of my intro to Java Projects. This project involves creating a GUI that has a list of trees and their possible heights, giving each tree a range for their possible minimum and maximum heights.
I am having trouble creating this method for the TreeDatabase (TreeDB class):
public String queryByPossibleHeight(int key) Instructions for this method are as follows: this method creates and returns a single string that contains the trees that can be found such that the value key is in the range for possible height. Traverse the entire ArrayList of Trees and for each one of the trees contained in the ArrayList invoke the method inRange() from the class Tree. Pass to the method inRange() the parameter key. If inRange() returns true, invoke the method toString() from the class Tree. Append the value returned by invoking the method toString() to the String result, adding a “” at the end. Once the entire ArrayList has been traversed, return the String result.
My 3 classes so far (I haven't gotten to the actual GUI yet):
public class Range {
private int low;
private int high;
private int value;
public Range(int plow,int phigh){
low = plow;
high = phigh;
[Code]...
As you can see in the last TreeDB class, in the public String queryByPossibleHeight(int key) method I have something wrote in, but I was just testing random things.
View Replies
View Related
Apr 9, 2014
What does the line Console[] conList mean? How am I going to access the string values in class consolelist?
class ConsoleInfo
{
private String conTitle;
private double conPrice;
private int conQty;
private String conPic;
[Code] .....
View Replies
View Related
Aug 10, 2014
How can I access the index of one character array and store those indexes into another array? I need this array of indices so as to perform an addition with another array.
Suppose I have a char array that stores all the letters of the alphabet (say alpha) and I have an another char array (say letter) that contains some letters in it. I want to retrieve those letters from the "letter" array and check its index in the "alpha" array and store that index into another integer array.
View Replies
View Related
Feb 5, 2015
Suppose I have private static void name() { ... } that has a two dimensional string array named array[. Now suppose I have private static void different() {...} and I want to write a condition where if (item == array) { ... }, how can I access my array from name() when I am in different()? I get a compile error saying cannot find symbol. My code is similar to:
Java Code:
public static void main(String[] args) {
...
String item = keyboard.nextLine();
... }
private static void name() {
...
String[][] array = new String[1][5];
[Code] .....
View Replies
View Related
Mar 18, 2014
How do I code this without having the need to use iterator? Code a Java method that accepts an ArrayList of integers and an integer. The method should delete all elements in the array list exactly divisible by the integer and return the number of remaining elements.
View Replies
View Related
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
Apr 29, 2014
Code:
public static void main(String[] args) {
// TODO code application logic here
Calendar time = Calendar.getInstance();
int min = 1;//time.get(Calendar.MINUTE);
String blank = "0";
int checker = ((min > 10 ) ? min : blank+min);
System.out.println("The time is " + "12" + ":" + checker );
}
This is my code, as you can see, I have if the min is less than 10, display the string "0" next to it so it will be something like
blank is zero and min is one
So it will display as 01 but after min reaches 10 and above, the 0 goes away. Problem I have is, you just cant add "blank" to int checker because checker is an int and blank is a string. So what must I do in order for it to display the 0 under checker?
View Replies
View Related