I have an Java LinkedList
which stores two values (code below).
I was wondering how i can obtain the 开发者_开发问答first value only?
I have:
LinkedList list = new LinkedList();
list.add(number+" "+string);
I want to be able to just print out number from the list. Is it possible?
First of all, note that by doing list.add(number + " " + string)
you're adding a single object, namely the string formed by concatenating number
and string
.
That said, you could try something like this, to get the first part of the first element:
list.get(0).split(" ")[0]
Example:
int number = 17;
String string = "hello";
LinkedList<String> list = new LinkedList<String>();
list.add(number+" "+string);
// Print number of first item:
System.out.println(list.get(0).split(" ")[0]); // prints "17"
I think you mean the number
only.
First get the object at index 0:
String node = list.get(0);
Then extract the first part by looking for the space
String num = node.substring(0,node.indexOf(' '));
Although I suggest looking into HashMaps
for such implementation, which takes Key,Value pairs:
For your example, you will need something like:
Map<Long,String> myMap = new HashMap<Long,String>();
myMap.put(number,string);
I'd suggest encapsulating your number and string into a class, like this.
public final class Pojo {
private final Number number;
private final String string;
public Pojo(Number number, String string) {
this.string = string;
this.number = number;
}
public void getNumber() {
return number;
}
public void getString() {
return string;
}
@Override
public String toString() {
return number + " " + string;
}
}
Then, create your List like this:
List<Pojo> list = new LinkedList<Pojo>(); // use generics for compile-time type safety
list.add(new Pojo(42, "Answer to the Ultimate Question of Life, the Universe, and Everything"); // add a Pojo
System.out.println(list.get(0).getNumber());
The List does not store two values. It stores one Object, the String created by concatenating number with a space and the String.
If you want to access individual Objects, you should either use a Map instead of a List or create a custom Object that holds number and string:
a)
Map<Integer, String> map = new Hashmap<Integer, String>();
map.put(number, string);
b)
public class MyObject{
private Number number;
private String string;
// + getters and setters
}
Also:
a) you should not use Non-generic Collections anymore. Generics were introduced 7 years ago, there should not be any reason not to use them these days.
So you should use this:
LinkedList<String> list = new LinkedList<String>();
Or this:
LinkedList<MyCustomObject> list = new LinkedList<MyCustomObject>();
b) are you sure you want a LinkedList
? In most cases, ArrayList
is the general purpose List
implementation. LinkedList
performs better if you have many add()
statements and don't know in advance how large your List will be, but ArrayList
performs a lot better for random access.
You are adding a string to that list. x + y, where either x or y is a string produces a string. If you want to have access to the number and the string, you should consider using another type - not a String here. Something like:
LinkedList<Pair<Number, String>> list = new LinkedList<Pair<Number, String>();
list.add(new Pair<Number, String>(number, string));
This way, to obtain the number, stored in the first element of the list:
Pair thePair = list.get(0);
number = (Number)thePair.left;
Pair is a class, you can find in Apache Commons: http://commons.apache.org/lang/api-3.0-beta/org/apache/commons/lang3/Pair.html (A source for many useful Java classes)
number+" "+string
Acts as a single string, if you want to get the number:
list.get(index).split(" ")[0]
Even if it works, this is not a clear way to manage a pair of values. From an object-oriented point of view (and this is the right context :D ), use directly an object:
class Pair {
int number;
String str;
}
Put your object to the list:
Pair pair = new Pair();
pair.number = 1;
pair.str = "hello";
list.add(pair);
And get your number:
((Pair)list.get(0)).number;
Of course you can use class methods and generics to manage all in a better way.
when you are initiating a Linkedlist in java, you should specify the data type that you are going to store in. You haven't done so. As your requirement it should be:
LinkedList<int> list=new LinkedList<>();
After that you can add your elements:
list.add(<number>);
Then you can get the first element:
list.getFirst();
Then you can print that.
精彩评论