2D Arraylist Replace Specified Elements
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
ADVERTISEMENT
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
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
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
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
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
Oct 15, 2014
I can't figure out why my code doesn't work. My task is to replace for example ä=>ae, using this method String.charAt(int index). So here is my code:
public class pich {
public static void main(String[] args) {
String text = "Die süße Hündin Leica läuft in die Höhle des fülligen Bären "+
"Iliyan (ein Übergrößenträger), der sie zum Teekränzchen eingeladen hat."+
" An ihrem Öhrchen trägt sie modisch eine Ähre.";
String textOhneUmlaute = "";
[Code] ....
when I launch my code I get the same String and nothing has changed
View Replies
View Related
Mar 2, 2015
How do you replace negative zero value with a zero value when Printing results:
I want to remove the minus sign in -0.0000 and instead have 0.0000.
I am Printing coordinates and I do not want to have negative zero.
the coordinates are defined as double.
System.out.format(java.util.Locale.US," %.4f %.4f %.4f %.4f%n"xCur ,yCur,xNext,yNext);
0.0000 1.0000 -0.0000 0.0000
View Replies
View Related
Aug 24, 2014
Basically, I am trying to write a method that will take a string, a target, and a replacement (a replacement function like string.replace). I want it to replace all instances of target with replacement EXCEPT for instances that occur within single or double quotes.
Example input / output:
this "Wont" be " replaced, be"
call: replace(theString, "replaced", "narf") and replace(theString, "be", "rafn")
output:
this "Wont" rafn " replaced, be"
View Replies
View Related
May 12, 2015
I am trying write a program that accept an input and present the input with every 5th character replaced with X (count spaces also).
For example:
input: "hello my name is mario"
outpu: "hellX my Xame Xi maXio"
I only manage to replace specific letters, e.g every "m" letter.
View Replies
View Related
Sep 11, 2014
I will like replace this code:
// boucle pour les fractions aleatoires
for (i=0; i<2; i++) {
// Creation des fractions aleatoires
Frac first = new Frac(r,5);
Frac second = new Frac(r,5);
[Code] .....
But when I use a switch case I do not have the same result as my four loops.
View Replies
View Related
Jan 8, 2014
How can I change the value of
String str = "1234|U||45|||"
into
String str = "1234|U| |45| | |"
View Replies
View Related
Mar 8, 2014
I'm trying to figure out the correct way to replace number into letter. In this case, I need two steps.
First, convert letter to number. Second, restore number to word.
Words list: a = 1, b = 2, f = 6 and k = 11.
I have word: "baafk"
So, for first step, it must be: "211611"
Number "211611" must be converted to "baafk".
But, I failed at second step.
Code I've tried:
public class str_number {
public static void main(String[] args){
String word = "baafk";
String number = word.replace("a", "1").replace("b","2").replace("f","6").replace("k","11");
System.out.println(word);
[Code] .....
Result for converting to number: baafk = 211611 But, result for converting above number to letter: 211611 = bkfk
What do I miss here?
How to distinguish if 11 is for "aa" and for "k"? D
View Replies
View Related
Nov 4, 2014
I have a coauthorship network and I want to replace the authors names with numbers.Part of my datadet:
1992,M_DINE,R_LEIGH,P_HUET,A_LINDE,D_LINDE
1992,C_BURGESS,J_CLINE,M_LUTY
1992,M_DINE,R_LEIGH,P_HUET,A_LINDE,D_LINDE
1992,F_ZWIRNER
1992,O_HERNANDEZ
...
I want the result to be sth like this:
1992 1,2,3,4
1992 5,67
....
and a table of mappings
M_DINE 1
R_LEIGH 2
P_HUET 3
....
I give my dataset in csv (or text) format to my program but it says "(The system cannot find the file specified)" even though the file exists.
what should I do?
here is my code:
Java Code: public class ReadCSV {
HashMap<String, Integer> authorList = new HashMap<>();
File file = new File("d:/Course/thesis/predict/whole.scv");
int authorCounter = 0;
public static void main(String[] args) {
ReadCSV obj = new ReadCSV();
[Code]...
View Replies
View Related
Jul 6, 2014
I was reading a book and came across this while loop.
public class Powers {
public static void main (String [] args){
int e;
int result;
for(int i = 0; i < 10; i++){
[Code] .....
This loop presents the following (I'm sure it's not necessary):
2 to the 0 power is 1
2 to the 1 power is 2
2 to the 2 power is 4
2 to the 3 power is 8
2 to the 4 power is 16
2 to the 5 power is 32
2 to the 6 power is 64
2 to the 7 power is 128
2 to the 8 power is 256
2 to the 9 power is 512
I am just having a difficult time understand and grasping this concept. My main issue is result *=2; this is making it very difficult to understand. How is result being replace if it only equals to 1.
View Replies
View Related
Jul 14, 2014
This is my code:
JDialog dialog = new JDialog();
dialog.setSize(400, 150);
dialog.setTitle("Input dialog");
dialog.add( new JLabel("simtime(min)") );
dialog.add( new JLabel("interval(sec)") );
dialog.setVisible(true);
The problem is that the two lables replace each other.How to position them where we want?
View Replies
View Related
Mar 8, 2014
I'm trying to figure out the correct way to replace number into letter. In this case, I need two steps.
First, convert letter to number. Second, restore number to word.
Words list: a = 1, b = 2, f = 6 and k = 11.
I have word: "baafk"
So, for first step, it must be: "211611"
Number "211611" must be converted to "baafk".
But, I failed at second step.
Code I've tried:
public class str_number {
public static void main(String[] args){
String word = "baafk";
String number = word.replace("a", "1").replace("b","2").replace("f","6").replace("k","11");
System.out.println(word);
[Code] ...
Result for converting to number: baafk = 211611
But, result for converting above number to letter: 211611 = bkfk
How to distinguish if 11 is for "aa" and for "k"? Do you have any solutions or other ways for this case?
View Replies
View Related
Jul 13, 2014
My interest in Java leads me to try and print numbers from 1-100. The output should show all numbers from 1-100 and each number that is divisible by by 13 should be replaced with a string "Fuzzy".
public class Fuzzy {
public static void main (String[]args){
for(int i = 0; i < 100; i++)
if(i % 13==0)
System.out.print(i + "fuzzy");
}
}
View Replies
View Related
Jul 21, 2014
I am trying to custom graphic class..,My task is to replace the specific area color of the image when i select the area & pick the color..,If i choose the hand / body of the T-shirts means the color can change by the user..,
View Replies
View Related
Jan 26, 2015
I am wondering if there is something similar to the "Find and Replace" action, that we use in applications like Eclipse and MS Word, that is actually built into the Java API so we can use it in our code, mainly for method names?
For example, right now I have these couple of lines
rotateLeftCounterClockwise(1);
rotateBottomClockwise(1);
rotateLeftClockwise(1);
//and
rotateRightCounterClockwise(1);
rotateBottomCounterClockwise(1);
rotateRightClockwise(1);
that does one operation for a side of Rubik's cube. However, all other sides of the cube can use these same exact code with the exception of the 2nd word, ie the position, in each method.
So for example, another side would do something, like, lets say
rotateFrontCounterClockwise(1);
rotateBottomClockwise(1);
rotateFrontClockwise(1);
//and
rotateBackCounterClockwise(1);
rotateBottomCounterClockwise(1);
rotateBackClockwise(1);
The reason why I ask this is because this would DRASTICALLY shrink the size my code, rather than have to create separate methods for each operation. Now I know there is a Find and Replace feature for Strings, but I am not sure about method names...
View Replies
View Related
Aug 8, 2014
I'm trying to replace html code with variables from a servlet. Lines with a * won't replace.
String t = screen_configedit.replace( "value="eID"", "value=""+ eid + """);
t = t.replace("value="cID"", "value=""+ cid + """);
t = t.replace("<!-- <br><font color="red">ERROR! Please contact support!</font> -->", "<br><font color="red">ERROR! Please contact support!</font>");
t = t.replace("value="status"", "value=""+ status + """);
[Code] .....
View Replies
View Related
Jan 29, 2015
My question is related to combo boxes. A combo box in general is a text field with a dropdown list. I would want to know if we can replace hthe dropdown List by an other component, for example a JTable component.
View Replies
View Related
Mar 1, 2014
I want to do a simple search and replace regular expression of lines. I am very unfamilar with Java regular expressions, and I'm not sure how to do something as simple as what I want to do. I have lines that look like this...
Java Code : access_log /home/%USER%/access_log mh_sh_highlight_all('java');
I want them changed so %USER% changes to a string, such as "cyrus," so they appear like this ...
Java Code: access_log /home/cyrus/access_log mh_sh_highlight_all('java');
The reason I want to use regular expressions is because I want to use the replaceAll method of the java.lang.String object. If I use replace I have to convert my strings into char arrays, and my code becomes bulky.
View Replies
View Related