• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 16:20
CEST 22:20
KST 05:20
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL19] Finals Recap: Standing Tall9HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0TL Team Map Contest #5: Presented by Monster Energy6
Community News
Flash Announces Hiatus From ASL49Weekly Cups (June 23-29): Reynor in world title form?12FEL Cracov 2025 (July 27) - $8000 live event16Esports World Cup 2025 - Final Player Roster16Weekly Cups (June 16-22): Clem strikes back1
StarCraft 2
General
The SCII GOAT: A statistical Evaluation Statistics for vetoed/disliked maps Esports World Cup 2025 - Final Player Roster How does the number of casters affect your enjoyment of esports? Weekly Cups (June 23-29): Reynor in world title form?
Tourneys
RSL: Revival, a new crowdfunded tournament series [GSL 2025] Code S: Season 2 - Semi Finals & Finals $5,100+ SEL Season 2 Championship (SC: Evo) FEL Cracov 2025 (July 27) - $8000 live event HomeStory Cup 27 (June 27-29)
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome Mutation # 478 Instant Karma Mutation # 477 Slow and Steady
Brood War
General
Flash Announces Hiatus From ASL [ASL19] Finals Recap: Standing Tall BGH Auto Balance -> http://bghmmr.eu/ Player “Jedi” cheat on CSL Help: rep cant save
Tourneys
Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL20] GosuLeague RO16 - Tue & Wed 20:00+CET The Casual Games of the Week Thread
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile What do you want from future RTS games? Beyond All Reason
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Trading/Investing Thread The Games Industry And ATVI
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2025 Football Thread NBA General Discussion Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023 NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Blogs
Culture Clash in Video Games…
TrAiDoS
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
StarCraft improvement
iopq
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 665 users

The Big Programming Thread - Page 600

Forum Index > General Forum
Post a Reply
Prev 1 598 599 600 601 602 1031 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2015-03-04 19:48:43
March 04 2015 19:26 GMT
#11981
== is fine for std::string comparisons in C++

it's a bit more readable than std::equal(stringA.first(), stringA.last(), stringB.first())


char * and char[] strings not so much...

There is no one like you in the universe.
Millitron
Profile Blog Joined August 2010
United States2611 Posts
March 04 2015 19:27 GMT
#11982
On March 05 2015 04:15 RoyGBiv_13 wrote:
Show nested quote +
On March 05 2015 03:54 travis wrote:
what is wrong with comparing strings with == or != ?


A String variable is effectively a pointer to memory where the characters that make up the string are located. By comparing Strings using == and !=, you're comparing the pointers.

In C, C++, and C#, this won't work 80% of the time, as the same string may be located at different memory locations. It will work on the occasion that you do:

typedef string char[];
string a = "foo";
string b = a;


In Java, Strings are immutable types. The Java interpreter knows of a special property with immutable types that it can use the same memory for identical Objects, so using the == and != in Java works since both String Objects point to the same place in memory.... sometimes. It's undefined how often or when it will work, since there are optimizations that depend on the length of the string, total number of Objects, memory used, generation in the garbage collection, etc.

In Python, the immutable types optimization comes up again, with the added difficulty of type coercion. Using "is" instead of "==" will result in the Strings actually being compared, where using "==" will check that they both point to the same String. If one does not point to a String, you may get NaN instead of True or False... !Fun!

In Javascript, "==" will coerce types causing certain corner cases to give odd results, such as NaN instead of False."===" will probably do the string equality you want instead.

In Java, Strings have a isEqual() method which you should use for comparing strings. There's also a compareTo() method for alphabetical order.
Who called in the fleet?
Logo
Profile Blog Joined April 2010
United States7542 Posts
March 04 2015 19:47 GMT
#11983
How do people feel about file naming across larger projects? Should file names be unique on search, or is it ok to rely on the folder path to distinguish different files and what trade-offs do people try to make?

I'm trying to write up some standards for file/folder structure, but am struggling a bit on how people should name file names.

As a rough example take a structure like: (In these examples bar/baz/other would be represent system objects or components rather than generic folders like "utils")

foo/bar/
  • key-bindings.x
  • my-class.x
  • another-class.x


foo/baz/
  • key-bindings.x
  • my-class.x


foo/ball/
  • key-bindings.x


utils
  • date-picker.x


vs. a scheme like:

foo/bar/
  • bar-key-bindings.x
  • bar-my-class.x
  • bar-another-class.x


foo/baz/
  • baz-key-bindings.x
  • baz-my-class.x


foo/ball/
  • ball-key-bindings.x


utils
  • date-picker.x


The tradeoff here is in the first scheme when viewed through an explorer tree or searched for under a constrained search context things are easy to distinguish and the filenames are shorter without losing any information when the context is viewed.

But in the latter case when people open files through an large net search or have multiple files open in multiple tabs the files are easier to distinguish and there's less confusion. Searching for "baz" in an open file dialog will quickly return results that are relevant rather than searching "key-bindings" and then having to sort through the potentially dozens of matches to see which one has the relevant folder path. This problem is especially pronounced if you have something like a module based folder path/packing where you may have ui/keybindings as the last part of the structure for every key-bindings.x file and users have to look at the middle part of the path to distinguish the files.
Logo
MichaelEU
Profile Joined February 2011
Netherlands816 Posts
Last Edited: 2015-03-04 20:06:18
March 04 2015 20:05 GMT
#11984
On March 05 2015 04:15 RoyGBiv_13 wrote:
In Python, the immutable types optimization comes up again, with the added difficulty of type coercion. Using "is" instead of "==" will result in the Strings actually being compared, where using "==" will check that they both point to the same String. If one does not point to a String, you may get NaN instead of True or False... !Fun!


Eh, no? "==" compares values, "is" compares identity. But you're right that some optimization occurs with immutable types.

+ Show Spoiler +

a = 1999
b = 1998 +1
>>> a is b
False
>>> a == b
True


Optimization:

a = 3
b = 2+1
>>> a is b
True
>>> a == b
True




To the above poster, stick to relying on folder path.
世界を革命する力を!― znf: "Michael-oniichan ( *^▽^*)ノ✩キラ✩"
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
March 04 2015 20:22 GMT
#11985
On March 05 2015 04:15 RoyGBiv_13 wrote:
In C, C++, and C#, this won't work 80% of the time, as the same string may be located at different memory locations. It will work on the occasion that you do:

It's perfectly fine in C# if you use the built in string type. It has its operators overloaded to behave like a value type except for the nullable part. So == is the actual string comparision. If you want to compare C# strings by reference, you'll have to use ReferenceEquals(string, string).
If you have a good reason to disagree with the above, please tell me. Thank you.
Ben...
Profile Joined January 2011
Canada3485 Posts
March 05 2015 02:08 GMT
#11986
On March 05 2015 04:26 Blisse wrote:
== is fine for std::string comparisons in C++

it's a bit more readable than std::equal(stringA.first(), stringA.last(), stringB.first())


char * and char[] strings not so much...


Yeah for character arrays as strings you're better of with strcmp or similar functions, but yeah for std::strings in C++, "==" is fine if you are concerned if the contents of two strings are the same. Likewise for C#. Java is the outlier here, where "==" fails and .equals() is required to compare them.

Of course references are a different beast but that's not what was being asked originally.
"Cliiiiiiiiiiiiiiiiide" -Tastosis
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2015-03-05 04:05:42
March 05 2015 03:53 GMT
#11987
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-03-05 04:10:16
March 05 2015 04:03 GMT
#11988
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?


ArrayList1.add( ArrayList2.getIndex( Object X ), ArrayList2.remove( ArrayList2.getIndex( Object X ) ) );
ArrayList2.add( ( ArrayList1.getIndex( Object Y ) - 1 ), ArrayList1.remove( ArrayList1.getIndex( Object Y ) ) );


Pretty sure that will work. The remove method returns the object which is the second parameter of the add method. The first parameter is the index where the object will be inserted. The second call requires a minus one on the index because the Object X is in the old position.
I'll always be your shadow and veil your eyes from states of ain soph aur.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
March 05 2015 04:09 GMT
#11989
Hard to wrap my brain around that LOL, but I will try it
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-03-05 04:23:38
March 05 2015 04:10 GMT
#11990
On March 05 2015 13:09 travis wrote:
Hard to wrap my brain around that LOL, but I will try it


I just mashed the add and remove calls into one statement so that there is no temp storage involved. If you know which index you're moving it gets a lot simpler as you can just use the number instead of the getIndex method each time. Keep in mind you'll need to add 1 to the index on the second call because the Object X now resides at the original index and the object that is to be moved got pushed back one index.

Object temp;
temp = ArrayList1.remove( index );
ArrayList2.add( index, temp );
temp = ArrayList2.remove( index + 1 );
ArrayList1.add( index, temp );


Code with known index with no temp
ArrayList1.add( index, ArrayList2.remove( index ) );
ArrayList2.add( index, ArrayList1.remove( index + 1 ) );


Being able to read your code is valuable though so be careful which corners you cut.
I'll always be your shadow and veil your eyes from states of ain soph aur.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
March 05 2015 04:41 GMT
#11991
On March 05 2015 13:03 Blitzkrieg0 wrote:
Show nested quote +
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?


ArrayList1.add( ArrayList2.getIndex( Object X ), ArrayList2.remove( ArrayList2.getIndex( Object X ) ) );
ArrayList2.add( ( ArrayList1.getIndex( Object Y ) - 1 ), ArrayList1.remove( ArrayList1.getIndex( Object Y ) ) );


Pretty sure that will work. The remove method returns the object which is the second parameter of the add method. The first parameter is the index where the object will be inserted. The second call requires a minus one on the index because the Object X is in the old position.


In the spirit of code you'll hate yourself for 2 days from now, can't you do -- assuming you already had obj1, obj2 you wanted to swap.

Basically goal here is at the end -
list2[obj2idx] = obj1
list1[obj1idx] = obj2

 
list2.set(list2.indexOf(obj2), list1.set(list1.indexOf(obj1), obj2))
RIP GOMTV. RIP PROLEAGUE.
ZenithM
Profile Joined February 2011
France15952 Posts
Last Edited: 2015-03-05 04:49:58
March 05 2015 04:44 GMT
#11992
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up

The solutions above are probably all correct, but you might want to wonder if you picked the right collection for the job. If you need swapping instances between 2 collections, and especially if you want to do it efficiently, you might want to choose something else, like, for example, regular arrays. "Remove" on ArrayLists is an inefficient operation (that is to say, it has about the worst complexity that you can have for such an elementary operation).
One other option is to change the object themselves (change them into each other) if they're not too complex and if what you're interested in is the value of their fields and not their physical location in memory.

Edit: I'd probably go as far as to say that if you need a whole bunch of swapping for an extended period of time, with no "add" in the meantime, depending on the size of your arraylists, it may be worth it to just copy the arraylists in arrays (with toArray methods).
In short, it all depends on what you're trying to do, if you just want to exchange cards between player hands, you can stay with arraylists, if you're trying to implement some swap-based sorting algorithm, copy into swap-friendly collections beforehand.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
Last Edited: 2015-03-05 05:13:05
March 05 2015 05:09 GMT
#11993
On March 05 2015 13:44 ZenithM wrote:
Show nested quote +
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up

The solutions above are probably all correct, but you might want to wonder if you picked the right collection for the job. If you need swapping instances between 2 collections, and especially if you want to do it efficiently, you might want to choose something else, like, for example, regular arrays. "Remove" on ArrayLists is an inefficient operation (that is to say, it has about the worst complexity that you can have for such an elementary operation).
One other option is to change the object themselves (change them into each other) if they're not too complex and if what you're interested in is the value of their fields and not their physical location in memory.

Edit: I'd probably go as far as to say that if you need a whole bunch of swapping for an extended period of time, with no "add" in the meantime, depending on the size of your arraylists, it may be worth it to just copy the arraylists in arrays (with toArray methods).
In short, it all depends on what you're trying to do, if you just want to exchange cards between player hands, you can stay with arraylists, if you're trying to implement some swap-based sorting algorithm, copy into swap-friendly collections beforehand.


Huh? ArrayList is backed by a...array. You don't need to do the solution with any calls to remove using an ArrayList, don't need to drop down to a raw array for that. Calling set means no calls to remove/no array resizing, you can read ArrayList code.


/**
* Replaces the element at the specified position in this list with
* the specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
rangeCheck(index);

E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}


Anyway, you can't really use a set/bag because those explictly are unordered collections and want order. The only problem is indexOf obviously isn't instant. I don't know best collection for you without knowing more of your code.

edit:
I mean I guess you have the range check call but ya that's pretty cheap. Obviously if this is the critical path of your hpc code, you'd be using fortran anyway, but outside that I don't think that it's worth dropping into a raw array for.

sorry if I'm missing some other good reason to use an array when you're otherwise using an arraylist.
RIP GOMTV. RIP PROLEAGUE.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
March 05 2015 05:22 GMT
#11994
Hi can someone help me out. Trying to generate an exe of a javafx app using maven. I see some stuff on the oracle site about the build task to add but I think that's for an ant build.

I can build the exe using the javafx packager cli tool, just would like to have it in the build process.

Here's my current POM - builds an executable Jar.

Also had a question about resources. I use some fxml files for layout (lol?) and so I have package com.example.passkeep, and the matching directory structure. The thing is now I ended up having the match that directory structure for my resources, (main/{src,resources}/com/example/passkeep), when for my resources I'd prefer to just have /main/resources/*.fxml.

+ Show Spoiler +


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 [url=http://maven.apache.org/maven-v4_0_0.xsd">]http://maven.apache.org/maven-v4_0_0.xsd">[/url]
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.passkeep</groupId>
<artifactId>pass-keep</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>pass-keep</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.passkeep.PassKeep</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

RIP GOMTV. RIP PROLEAGUE.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
March 05 2015 05:32 GMT
#11995
On March 05 2015 04:15 RoyGBiv_13 wrote:
Show nested quote +
On March 05 2015 03:54 travis wrote:
what is wrong with comparing strings with == or != ?


A String variable is effectively a pointer to memory where the characters that make up the string are located. By comparing Strings using == and !=, you're comparing the pointers.

In C, C++, and C#, this won't work 80% of the time, as the same string may be located at different memory locations. It will work on the occasion that you do:

typedef string char[];
string a = "foo";
string b = a;


In Java, Strings are immutable types. The Java interpreter knows of a special property with immutable types that it can use the same memory for identical Objects, so using the == and != in Java works since both String Objects point to the same place in memory.... sometimes. It's undefined how often or when it will work, since there are optimizations that depend on the length of the string, total number of Objects, memory used, generation in the garbage collection, etc.

In Python, the immutable types optimization comes up again, with the added difficulty of type coercion. Using "is" instead of "==" will result in the Strings actually being compared, where using "==" will check that they both point to the same String. If one does not point to a String, you may get NaN instead of True or False... !Fun!

In Javascript, "==" will coerce types causing certain corner cases to give odd results, such as NaN instead of False."===" will probably do the string equality you want instead.


In Java is it really undefined? Obviously I trust you as knowing a lot more about programming stuff then me based on your post histry, but Java doesn't have much undefined behavior I can think of unlike C/C++

I thought string literals you define in code are part constant pool so == work (along with the integer pool it maintains and stuff like that. Used to be perm gen but in Java 8 with perm gen gone idk how the whole constant pool works).

Any string you get otherwise won't compare with == unless you explicitly intern them?

POC.
code
+ Show Spoiler +


public class Main {

public static void main (String[] beans) {
String s1 = "test";
String s2 = "test";
Scanner s = new Scanner(System.in);
String s3 = s.next();

System.out.printf("%s: %d\n", s1, s1.hashCode());
System.out.printf("%s: %d\n", s2, s2.hashCode());
System.out.printf("%s: %d\n", s3, s3.hashCode());

System.out.println("s1 == s2");
System.out.println(s1 == s2);

System.out.println("s1.equals(s2)");
System.out.println(s1.equals(s2));

System.out.println("s1 == s3");
System.out.println(s1 == s3);

System.out.println("s1.equals(s3");
System.out.println(s1.equals(s3));

String interned = s3.intern();
System.out.println("s1 == s3");
System.out.println(s1 == interned);
}

}



run
+ Show Spoiler +


test
test: 3556498
test: 3556498
test: 3556498
s1 == s2
true
s1.equals(s2)
true
s1 == s3
false
s1.equals(s3
true
s1 == s3
true



RIP GOMTV. RIP PROLEAGUE.
ZenithM
Profile Joined February 2011
France15952 Posts
Last Edited: 2015-03-05 06:10:12
March 05 2015 06:03 GMT
#11996
On March 05 2015 14:09 teamamerica wrote:
Show nested quote +
On March 05 2015 13:44 ZenithM wrote:
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up

The solutions above are probably all correct, but you might want to wonder if you picked the right collection for the job. If you need swapping instances between 2 collections, and especially if you want to do it efficiently, you might want to choose something else, like, for example, regular arrays. "Remove" on ArrayLists is an inefficient operation (that is to say, it has about the worst complexity that you can have for such an elementary operation).
One other option is to change the object themselves (change them into each other) if they're not too complex and if what you're interested in is the value of their fields and not their physical location in memory.

Edit: I'd probably go as far as to say that if you need a whole bunch of swapping for an extended period of time, with no "add" in the meantime, depending on the size of your arraylists, it may be worth it to just copy the arraylists in arrays (with toArray methods).
In short, it all depends on what you're trying to do, if you just want to exchange cards between player hands, you can stay with arraylists, if you're trying to implement some swap-based sorting algorithm, copy into swap-friendly collections beforehand.


Huh? ArrayList is backed by a...array. You don't need to do the solution with any calls to remove using an ArrayList, don't need to drop down to a raw array for that. Calling set means no calls to remove/no array resizing, you can read ArrayList code.


/**
* Replaces the element at the specified position in this list with
* the specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
rangeCheck(index);

E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}


Anyway, you can't really use a set/bag because those explictly are unordered collections and want order. The only problem is indexOf obviously isn't instant. I don't know best collection for you without knowing more of your code.

edit:
I mean I guess you have the range check call but ya that's pretty cheap. Obviously if this is the critical path of your hpc code, you'd be using fortran anyway, but outside that I don't think that it's worth dropping into a raw array for.

sorry if I'm missing some other good reason to use an array when you're otherwise using an arraylist.

Yes, I know arraylist is backed by an array, I guess I was arguing against the "insert + remove" method specifically, and that I had forgotten "set" for some reason :D. Still my point stands for "insert + remove", it's just not very good. And for other implementations of the List interface, "set" would be implemented as "insert + remove", so it's never a bad thing to be careful of.
Edit: Hmm yeah, I just hadn't seen the code in your post.
On March 05 2015 13:41 teamamerica wrote:
Show nested quote +
On March 05 2015 13:03 Blitzkrieg0 wrote:
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?


ArrayList1.add( ArrayList2.getIndex( Object X ), ArrayList2.remove( ArrayList2.getIndex( Object X ) ) );
ArrayList2.add( ( ArrayList1.getIndex( Object Y ) - 1 ), ArrayList1.remove( ArrayList1.getIndex( Object Y ) ) );


Pretty sure that will work. The remove method returns the object which is the second parameter of the add method. The first parameter is the index where the object will be inserted. The second call requires a minus one on the index because the Object X is in the old position.


In the spirit of code you'll hate yourself for 2 days from now, can't you do -- assuming you already had obj1, obj2 you wanted to swap.

Basically goal here is at the end -
list2[obj2idx] = obj1
list1[obj1idx] = obj2

 
list2.set(list2.indexOf(obj2), list1.set(list1.indexOf(obj1), obj2))

Edit2: Still, indexOf suffers from the same problem as remove (I know that you already know this teamamerica, I'm just saying that for travis :D). It's either you already know what indices you want to swap objects at (which I do guess is the case for travis), or you use another collection.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
March 05 2015 06:31 GMT
#11997
On March 05 2015 15:03 ZenithM wrote:
Show nested quote +
On March 05 2015 14:09 teamamerica wrote:
On March 05 2015 13:44 ZenithM wrote:
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up

The solutions above are probably all correct, but you might want to wonder if you picked the right collection for the job. If you need swapping instances between 2 collections, and especially if you want to do it efficiently, you might want to choose something else, like, for example, regular arrays. "Remove" on ArrayLists is an inefficient operation (that is to say, it has about the worst complexity that you can have for such an elementary operation).
One other option is to change the object themselves (change them into each other) if they're not too complex and if what you're interested in is the value of their fields and not their physical location in memory.

Edit: I'd probably go as far as to say that if you need a whole bunch of swapping for an extended period of time, with no "add" in the meantime, depending on the size of your arraylists, it may be worth it to just copy the arraylists in arrays (with toArray methods).
In short, it all depends on what you're trying to do, if you just want to exchange cards between player hands, you can stay with arraylists, if you're trying to implement some swap-based sorting algorithm, copy into swap-friendly collections beforehand.


Huh? ArrayList is backed by a...array. You don't need to do the solution with any calls to remove using an ArrayList, don't need to drop down to a raw array for that. Calling set means no calls to remove/no array resizing, you can read ArrayList code.


/**
* Replaces the element at the specified position in this list with
* the specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
rangeCheck(index);

E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}


Anyway, you can't really use a set/bag because those explictly are unordered collections and want order. The only problem is indexOf obviously isn't instant. I don't know best collection for you without knowing more of your code.

edit:
I mean I guess you have the range check call but ya that's pretty cheap. Obviously if this is the critical path of your hpc code, you'd be using fortran anyway, but outside that I don't think that it's worth dropping into a raw array for.

sorry if I'm missing some other good reason to use an array when you're otherwise using an arraylist.

Yes, I know arraylist is backed by an array, I guess I was arguing against the "insert + remove" method specifically, and that I had forgotten "set" for some reason :D. Still my point stands for "insert + remove", it's just not very good. And for other implementations of the List interface, "set" would be implemented as "insert + remove", so it's never a bad thing to be careful of.
Edit: Hmm yeah, I just hadn't seen the code in your post.
Show nested quote +
On March 05 2015 13:41 teamamerica wrote:
On March 05 2015 13:03 Blitzkrieg0 wrote:
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?


ArrayList1.add( ArrayList2.getIndex( Object X ), ArrayList2.remove( ArrayList2.getIndex( Object X ) ) );
ArrayList2.add( ( ArrayList1.getIndex( Object Y ) - 1 ), ArrayList1.remove( ArrayList1.getIndex( Object Y ) ) );


Pretty sure that will work. The remove method returns the object which is the second parameter of the add method. The first parameter is the index where the object will be inserted. The second call requires a minus one on the index because the Object X is in the old position.


In the spirit of code you'll hate yourself for 2 days from now, can't you do -- assuming you already had obj1, obj2 you wanted to swap.

Basically goal here is at the end -
list2[obj2idx] = obj1
list1[obj1idx] = obj2

 
list2.set(list2.indexOf(obj2), list1.set(list1.indexOf(obj1), obj2))

Edit2: Still, indexOf suffers from the same problem as remove (I know that you already know this teamamerica, I'm just saying that for travis :D). It's either you already know what indices you want to swap objects at (which I do guess is the case for travis), or you use another collection.



Sorry if I came off as too combative.

Misunderstanding a few points:
>And for other implementations of the List interface, "set" would be implemented as "insert + remove", so it's never a bad thing to be careful of.

Set isn't implementation of List interface. But we're might be talking cross points here.

>indexOf suffers from the same problem as remove...

indexOf suffers from same probem as remove? Are you talking about it being O(n)? My bad - thought you were bringing up insert/removing triggering array resizing.

But I don't know how you'd work around the need to do this

indexOf isn't O(1) but neither would it be for an array b/c you still need to find the damn index in the array, unless you already know indices, in which case you don't need to use indexOf for the ArrayList method anyway (l1.set(o2idx,list.set(o1idx, l2.get(o2idx))), now that's all constant time operations.

>It's either you already know what indices you want to swap objects at (which I do guess is the case for travis), or you use another collection.

The code I posted assumes you don't know the indices. indexOf kind of sucks, but I don't know what better way there is to do it really, because I'm assuming the ordering of the list matters somehow, so I can't imagine easily dropping in other collection? Maybe having another bookeeping map where you track index of each object, combined with the list, but that has it's own tradoffs.

What other collection would you use? I get this feeling I'm missing some obvious way to have an arbitrarily ordere collection where you also know in O(1) or even O(log(n)) the index of an item in the collection (collections not sorted so you can't binary search).


Again, sorry if I'm misunderstanding something.
RIP GOMTV. RIP PROLEAGUE.
ZenithM
Profile Joined February 2011
France15952 Posts
Last Edited: 2015-03-05 08:23:34
March 05 2015 08:20 GMT
#11998
You can definitely implement some indexed collection over a hashtable with Integer keys, with another hashtable for reverse indexing. Not saying you should (;D), but you can, and indexOf would be O(1) in this case, as well as set, so your method would be in constant time. (There are also restrictions like being careful because you can't put the same object twice or shit like that)

>And for other implementations of the List interface, "set" would be implemented as "insert + remove", so it's never a bad thing to be careful of.

Set isn't implementation of List interface. But we're might be talking cross points here.

I was talking about the method set(int, T) of List<T>, not sets, sorry for that. For example you can use set on LinkedList as you would on ArrayList, but then it's surely in O(n) and not O(1).

Again, sorry if I'm misunderstanding something.

No worries mate, my point aren't very strong anyway, you were right in the first place
berated-
Profile Blog Joined February 2007
United States1134 Posts
March 05 2015 12:02 GMT
#11999
On March 05 2015 14:22 teamamerica wrote:
Hi can someone help me out. Trying to generate an exe of a javafx app using maven. I see some stuff on the oracle site about the build task to add but I think that's for an ant build.

I can build the exe using the javafx packager cli tool, just would like to have it in the build process.

Here's my current POM - builds an executable Jar.

Also had a question about resources. I use some fxml files for layout (lol?) and so I have package com.example.passkeep, and the matching directory structure. The thing is now I ended up having the match that directory structure for my resources, (main/{src,resources}/com/example/passkeep), when for my resources I'd prefer to just have /main/resources/*.fxml.

+ Show Spoiler +


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 [url=http://maven.apache.org/maven-v4_0_0.xsd">]http://maven.apache.org/maven-v4_0_0.xsd">[/url]
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.passkeep</groupId>
<artifactId>pass-keep</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>pass-keep</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.passkeep.PassKeep</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>



I have never tried it so no promises if it works but it looks like this might work:

http://zenjava.com/javafx/maven/native-bundle.html

As far as the classpath location goes, how are you actually referencing your fxml files? I just took a 15 second tutorial on fx and it shows something like this...

Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));


If that is what you are doing too, then, you just need to change how you are getting the resource. Maven requires that you split your classpath files into two parts, src/main/java and src/main/resources .. where as you know resources is for non java files. All of the stuff ends up on the classpath in the end though, so when you are getting the resource you just need to load it from the correct spot if you want to move it.

This might help explain it a bit more:

Link to stackoverflow post
heartlxp
Profile Joined September 2010
United States1258 Posts
March 05 2015 18:20 GMT
#12000
hi guys, anyone else have problem with frontend caching?

we make changes to html/js, but we have to clear cache in our browsers to see them. this happens generally for modals and dropdown menus. we use Angularjs/nginx if that's relevant at all.

when developing it's trivial to refresh, but we can't force all users to clear cache every time we update...any ideas?
Prev 1 598 599 600 601 602 1031 Next
Please log in or register to reply.
Live Events Refresh
Next event in 3h 40m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 596
JuggernautJason94
StarCraft: Brood War
Britney 14479
Dewaltoss 192
Aegong 45
GoRush 13
IntoTheRainbow 7
yabsab 5
Dota 2
Gorgc7367
League of Legends
Dendi1481
JimRising 443
Counter-Strike
fl0m1928
flusha408
Foxcn365
sgares185
Super Smash Bros
Mew2King179
Heroes of the Storm
Liquid`Hasu605
Other Games
summit1g6033
FrodaN2371
tarik_tv1112
elazer248
RotterdaM161
Pyrionflax116
ViBE84
Trikslyr64
Sick62
PPMD12
Liquid`Ken5
Organizations
Other Games
BasetradeTV30
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• LUISG 10
• intothetv
• IndyKCrew
• sooper7s
• Migwel
• AfreecaTV YouTube
• LaughNgamezSOOP
• Kozan
StarCraft: Brood War
• blackmanpl 38
• 80smullet 29
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
League of Legends
• Doublelift2783
• Jankos2203
• masondota2502
Other Games
• imaqtpie873
• Shiphtur187
Upcoming Events
Replay Cast
3h 40m
RSL Revival
13h 40m
herO vs SHIN
Reynor vs Cure
OSC
16h 40m
WardiTV European League
19h 40m
Scarlett vs Percival
Jumy vs ArT
YoungYakov vs Shameless
uThermal vs Fjant
Nicoract vs goblin
Harstem vs Gerald
FEL
19h 40m
Korean StarCraft League
1d 6h
CranKy Ducklings
1d 13h
RSL Revival
1d 13h
FEL
1d 19h
Sparkling Tuna Cup
2 days
[ Show More ]
RSL Revival
2 days
FEL
2 days
BSL: ProLeague
2 days
Dewalt vs Bonyth
Replay Cast
4 days
Replay Cast
4 days
The PondCast
5 days
Replay Cast
6 days
RSL Revival
6 days
Liquipedia Results

Completed

Proleague 2025-06-28
HSC XXVII
Heroes 10 EU

Ongoing

JPL Season 2
BSL 2v2 Season 3
BSL Season 20
Acropolis #3
KCM Race Survival 2025 Season 2
CSL 17: 2025 SUMMER
Copa Latinoamericana 4
Championship of Russia 2025
RSL Revival: Season 1
Murky Cup #2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025
YaLLa Compass Qatar 2025

Upcoming

CSLPRO Last Chance 2025
CSLPRO Chat StarLAN 3
K-Championship
uThermal 2v2 Main Event
SEL Season 2 Championship
FEL Cracov 2025
Esports World Cup 2025
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.