here i am trying to add elements to the array.
the elements i am trying to add are text fields, so im basically trying to store persons contact details within the array list?
any help would be greatful
public void addContact()
{
ArrayList<String> details = new ArrayList<String>();
{
details.get(txtname(0));
details.get(txtnum(1));
details.get(txtmob(2));
details.get(txtadd1(3));
}
}
It sounds like you haven't thought out the entire problem yet.
Adding elements to an ArrayList in Java is done like this:
public void addContact(){
ArrayList<String> foo = new ArrayList<String>();
foo.add("HELLO");
foo.add("WORLD");
}
yankee2905 explains it very well; that's what you need to get your code to work with an ArrayList.
As a side note, you're not dealing with an array, you're dealing with an ArrayList. For an array, you might have something like this:
String[] details = new String[4];
details[0] = "First";
details[1] = "Second";
details[2] = "Third";
details[3] = "Last";
It almost sounds like you're trying to use an ArrayList to store contact information for multiple people. If that is the case, you will probably want to do it a bit differently. You can create a Contact object that has members for each piece of information you want to store (e.g. firstname, lastname, phone, mobile, address1, address2, etc). Then you can just add Contact objects to your ArrayList like:
Contact contact1 = new Contact();
contact1.setFirstname("Bob");
myArrayList.add(contact1);
public void addContact()
{
ArrayList<String> details = new ArrayList<String>();
{
details.add(//Insert value from post here);
details.add(//Insert value from post here);
details.add(//Insert value from post here);
details.add(//Insert value from post here);
}
}
I've not used java in a while maybe someone will add to this.
You need set or add, not get. See the docs here.
And to get the text from the textfields, use getText.
So you'd have something like:
myArrayList.add(myTextField.getText());
You are trying to use inbuilt array initializer syntax. That does not work on container classes (unless its some new fangled way in c#) you need to use details.add() (or the appropriate member function).
The syntax you are trying to use is for the language supported hardwired array types. In C++ this would look like char x[6] = {'h','e','l','l','o'};. However a container is not an array its a container object. Container objects often mimic arrays by overloading operator[] however they use different data structures behind the scenes -- i.e., they do not use contiguous regions of memory.
p.s., If this was c#.NET -- which I initially assumed -- there is a new mechanism to map array initialization to container object creation. I'll leave it down there for anyone that is interested.
in C# 3.5 using array initializer syntax you can do the following :
public void addContact()
{
ArrayList<String> details = new ArrayList<String>()
{
details.get(txtname(0)),
details.get(txtnum(1)),
details.get(txtmob(2)),
details.get(txtadd1(3))
}
}
Gotta love Microsoft and C# :P
public void addContact()
{
ArrayList<String> details = new ArrayList<String>();
details.add(txtname.getText());
details.add(txtnum.getText());
details.add(txtmob.getText());
details.add(txtadd1.getText());
}
Sorry I don't have an IDE open, but I think this is closer to what you are after.
I think this is the best solution if you want to create an array with some elements:
String[] images = {"a.png","b.png","c.png"};
or
String[] images;
images = new String[]{"a.png","b.png","c.png"};
Related
I've got a function to create syllables for words.
I use it like this: syllables(word1field); - creates List with syllables: aa,bb,cc
and syllables(word2field); - creates List with syllables: dd,ee,ff
And in the result I get dd,ee,ff, but I need aa,bb,cc,dd,ee,ff.
Is there possibility to append second list to first?
You get dd,ee,ff because when you call the same method again, it overrides the first ArrayList that is created.
The best thing you could do, that I can think of, is to make your ArrayList global because currently you just keep getting rid of the previous values and create a new ArrayList with the new values you give it. Try doing something like:
public class MyClass {
private List<String> myArray;
public MyClass() {
myArray = new ArrayList<String>();
}
public void syllables(wordfield) {
// do whatever you need to with wordfield
myArray.add(syllable);
}
I don't know how you've got everything laid out but this is the best solution I can think of.
I have following lines of code:
private ArrayList<wordClass>[] words;
and
public class wordClass {
public String wordValue = null;
public int val = 0;
public boolean used = false;
}
Is there anyway I can access wordValue, val, and used via words? Like words[5].val? I know I can do that if they are just in an array of wordClass, but I want a dynamic array to make it easier to add and subtract from the array.
And yes, I know the values should be private. Just don't want to write getters and setters yet.
Thanks.
Do you really want an Array of an ArrayList?
It doesn't seem correct.
In Arrays, you use [] to access (words[0]).
In ArrayLists, you should use words.get(0).
The way you have coded, you should use: words[0].get(0).val to get the very first value.
But I recommend you to review your words definition.
ArrayList Documentation: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Regards,
Bruno
Your code is a bit off for a dynamic array (Java has immutable arrays), so you need an ArrayList. Also, Java uses Capital Letters for class names (please follow the convention) -
// like this, changing wordClass to WordClass. Also, using the diamond operator
private ArrayList<WordClass> words = new ArrayList<>();
To access your WordClass fields you can use something like -
for (WordClass wc : words) {
if (wc.used) {
System.out.println(wc.wordValue + " = " + wc.val);
}
}
Note, you still need to create WordClass instances and place them into the words List.
Write wrapper classes for each value. e.g. What you call "getters".
Then call:
words[1].getWordValue() ==> None
Voila
I want to be able to make an array list of objects, and then be able to edit and read back some simple properties of each object. In this case, a String (colour) and integer (X). I can't seem to make the simple code below work. Note that I am aware people sometimes use the <> notation with array lists, but I have read that it should be possible without, and at the moment I am very new to Java and wish to keep things as simple as possible.
At the moment I get an error on the line which is commented out (cannot find symbol X)
import java.util.ArrayList;
public class ArrayList_of_Objects {
public static void main(String[] args)
{
ArrayList al = new ArrayList();
for(int i =0;i<5;i++)
{
MyObj ob1 = new MyObj();
ob1.X = i + 5;
al.add(ob1);
//System.out.println("X: "+al.get(i).X);
}
for(int j=0;j<5;j++)
{System.out.println("X: "+al.get(j).X);}
al.get(3).X=4;
al.get(3).colour="orange";
System.out.println(al.get(3).X);
System.out.println(al.get(3).colour);
}
}
class MyObj
{
int X;
String colour;
}
Yes, you need to use generics to say what kind of object will be in your list:
ArrayList<MyObj> al = new ArrayList<MyObj>();
Otherwise the compiler doesn't know that al.get(3) is meant to return a MyObj rather than just anything. With this code, it will not only fix the get calls - it will stop you from accidentally adding objects of inappropriate types to the list.
Also, it's usually preferred to declare variables as an appropriate interface type, such as:
List<MyObj> al = new ArrayList<MyObj>();
(There are times where you really need it to be the concrete type, but generally prefer programming to interfaces.)
Generics is a massive topic, but the tutorial linked at the start of this answer should get you going. The Java Generics FAQ is a great resource for finding out more.
Change
ArrayList al = new ArrayList();
to
ArrayList<MyObj> al = new ArrayList<MyObj>();
What you have right now is an array list of Object rather than an array list of MyObj, and Object has no knowledge of MyObj.X.
Use ArrayList<MyObj> al = new ArrayList<MyObj>(); instead
I have an ArrayList of my own class Case. The class case provides the method getCaseNumber() I want to add all of the cases casenumber to a String[] caseNumber. I've tried this
public String[] getCaseNumberToTempList(ArrayList<Case> caseList) {
String[] objectCaseNumber = null;
for(int i = 0; i < caseList.size(); i++) {
objectCaseNumber[i] = caseList.get(i).getCaseNumber();
}
return objectCaseNumber;
}
But my compiler complaints about that the objectCaseNumber is null at the point insid the for-loop. How can I manage to complete this?
Well, you need to create an array to start with, and initialize the variable with a reference to the array. (See the Java tutorial for arrays for more information.) For example:
String[] objectCaseNumber = new String[caseList.size()];
Alternatively, build a List<String> (e.g. using ArrayList) instead. That's more flexible - in this case it's simple as you know the size up front, but in other cases being able to just add to a list makes life a lot simpler.
In idiomatic Java, you wouldn't use ArrayList as a parameter type. Use List.
Slightly more overhead, but simpler and more readable code is to accumulate in another List and then convert into an arrray:
public String[] getCaseNumberToTempList(List<Case> caseList) {
final List<String> r = new ArrayList<String>();
for (Case c : caseList) r.add(c.getCaseNumber());
return r.toArray(new Case[0]);
}
In your code it does make sense to insist on ArrayList due to performance implications of random access via get, but if you use this kind of code (and I suggest making a habit of it), then you can work with any List with the same results.
Well, as I think you may have misunderstood Arrays as a primitive type. Arrays in java are objects and they need to be initialized before you access it.
I have constructed a class to mimic a C# struct:
public class Favourite {
protected String favName;
protected String favText;
protected String favDelay;
protected GeoPoint favPoint;
protected Uri favUri;
}
I want to create an array of this class:
Favourite[] fav;
When I try to access this array:
fav[s].favName = bufr;
I get a NullPointerException. bufr does contain data. I have tracked it down to accessing the array as the following code:
fav[s].favName = "";
also produces a NullPointerException.
I have searched high and low for some indication as to whether or not what I am doing is allowed but cannot find anything.
I suppose my questions are:
Are you allowed to create an array of a class object?
If so, how do you refer to that array?
I know I could do this using five separate arrays of the variables but I feel that putting them into a class gives a better structure and is more elegant (I like elegance).
The problem is that fav[s] is null.
I don't know about C#, but in Java, you have to initialize the elements of the array individually; you can't just declare the array and get it automatically filled.
You're going to have to loop through fav and fill it with new Favourite objects.
Either assign fav[s] = new Favourite() the first time you use fav[s], or initialize it all at once by doing
for (int i = 0; i < fav.length; i++) {
fav[s] = new Favourite();
}
Favourite[] fav = new Favourite[23]; // Allocate an array of 23 items
Now you have 23 of them!
You need to put items into the array. The declared array simply has null in each slot; you need to do something like fav[s] = new Favourite().