Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with splice  RSS feed

 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use splice to delete elements out of an array. My array is and object array and is named system_task_objects. The index to delete is selected by user input. I've never used splice before and I don't know where I am going wrong.

Code:
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of object did you say system_task_objects was? Arrays don't have any method called splice(). The only method by that name I could find is a static method in a class called NativeArray, which looks to be an internal jdk class that's part of the javascript support added a little while ago (package is jdk.nashorn.internal.objects).

Judging by the rest of the code you've shown there, I'm thinking that's probably not what you want to be using. Consider using a List for your objects and taking a look at the remove() method.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using remove(), but I think I get the same problem. It cannot find remove()

If you care to look, this link talks about splice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link you shared is a reference for the JavaScript language, not Java. Two very different things
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to know.

I tried using remove(), but that still doesn't work.


 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How have you declared system_task_objects?
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I declared it globally.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the declaration look like? And what do you mean when you say it still doesn't work? What happens? Do you get an error?
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott M Summers wrote:Yes, I declared it globally.
No, you didn't, at least not in Java®, because Java® does not support global declarations. I presume you are using Java® and not JavaScript, which is an unrelated language?
You may have declared a public variable somewhere, which is not a global variable, but is has all the problems of global variables.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am referring to Java. And are you saying there are no global variables in Java? And do you have any information that relates to my question?
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott M Summers wrote:I am referring to Java. And are you saying there are no global variables in Java? And do you have any information that relates to my question?


Campbell is right. In Java, every variable must be declared in a class. You can't have a "global" variable the way you can in some other languages.

As far as information related to your question, I don't think there's much we can do to help without seeing what system_task_objects looks like and what you are encountering when you say it "still doesn't work."
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell is right. In Java, every variable must be declared in a class. You can't have a "global" variable the way you can in some other languages.


That's interesting, that is not what my CS teachers have taught. Thanks for the info.

On line 3 is where I declare system_task_objects
And by, "it doesn't work" I mean that I get an error using remove(). It cannot find remove().


 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's true. The compiler can't find the remove() method because arrays in Java don't have a remove() method. In fact there isn't any way in Java to remove an entry from an array; an array has a fixed length which can't be changed, which means that removing entries (and adding entries as well) isn't possible.

That's another difference between Javascript and Java.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays in java do not have a splice method or a remove method. You can't change the size of an array, and there is no in built support for removing elements from an array.

The advice you were given was to switch to using a List (which does have a remove method). However if this is an assignment you may have been given instructions to use an array.

Where did you get the idea about splicing? And what instructions were you given?
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that explains it. So to "delete" in an array, shifting would be the answer?
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the name being incorrect style, that is a private field. There is nothing global about it. Remove the keyword static from everywhere you can. The one place you can't get rid of it is in the header for the main method.
There is something wrong with your declarations for the chars in lines 13‑16.

You have also not written a constructor in that class. You think you have but you haven't.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:Arrays in java do not have a splice method or a remove method. You can't change the size of an array, and there is no in built support for removing elements from an array.

The advice you were given was to switch to using a List (which does have a remove method). However if this is an assignment you may have been given instructions to use an array.

Where did you get the idea about splicing? And what instructions were you given?


I google'd how to delete an index in an array and I found splicing.
And yes, I have to use an array.
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott M Summers wrote: . . .
I google'd how to delete an index in an array and I found splicing.
. . .
Unfortunately, the search has led you to the wrong place. It is unfortunate that you have to use arrays because your array has 16 empty places which point to null, and nulls are potentially dangerous. You need to avoid using element 11 for example. I suggest you have a count variable which counts how many elements the array contains which are “real”, so whenever you add an element you increment count and vice versa. Also find out about copying parts of arrays. Look in the System class.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Splicing is available in some other languages. Python has it for example. Java doesn't though.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your assignment?
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've declared my array within WorkOrders(), and I'll pass the array as arguments.





But I'm having a problem calling the method with the arguments

I've tried to call the method with (String[]system_task_objects) & (system_task_objects) neither work.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't access it because the system_task_objects is only in scope in your WorkOrders() method. As Campbell pointed out earlier, I think you intended that method to be a constructor. You should read up on how to declare those and how write a class correctly. That should help solve your current problem.

Also, as a side note: you should use camelCase for naming your variables (so systemTaskObjects and not system_task_objects).
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The type and location of that array keep changing.
 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Bullers wrote:You can't access it because the system_task_objects is only in scope in your WorkOrders() method. As Campbell pointed out earlier, I think you intended that method to be a constructor. You should read up on how to declare those and how write a class correctly. That should help solve your current problem.

Also, as a side note: you should use camelCase for naming your variables (so systemTaskObjects and not system_task_objects).

Campbell Ritchie wrote:The type and location of that array keep changing.


Thanks for your help. I will study up on constructors.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming it's contained in a switch statement, you can't do

However you can do


Why is your array an Object[] instead of a String[] ? [Edit: never mind. I see you've changed it]
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:. . . you can't do. . .
Yes, you can. That is the problem. But it doesn't do what you think it does.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott M Summers wrote:[That's interesting, that is not what my CS teachers have taught.

Which makes me wonder if they're actually trained in Java. The terminology you're using - and the way you're coding your variable names - suggests that they're "importing" techniques and styles (and assumptions) from other languages into Java. But I could be wrong.

However, a quick tip for you: If you change:
  userMenuSelection = reader.next().charAt(0);
to:
  userMenuSelection = reader.next().toLowerCase().charAt(0);
You won't need all those '|'s in your case statement - which are illegal in the context your using them anyway.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . all those '|'s in your case statement - which are illegal in the context your using them anyway. . . .
No, they are perfectly legal. That code will compile happily. It will run too, and do what the OP doesn't expect.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!